Friday, September 5, 2025
HomeLanguagesReturn a boolean array which is True where the string element in...

Return a boolean array which is True where the string element in array ends with suffix in Python

In this article, we are going to see how we will return a boolean array which is True where the string element in the array ends with a suffix in Python.

numpy.char.endswith()

numpy.char.endswith() return True if the elements end with the given substring otherwise it will return False.

Syntax : np.char.endswith(input_numpy_array,’substring’)

Parameters:

  •  input_numpy_array refers to the numpy array with strings
  •  substring is compared with all elements present in an array

Return: Return the boolean array which includes “True” if a substring is present as a suffix and “False” if a substring is not present as a suffix.

Example 1: 

In this example, we are creating a NumPy array with 5 strings and checking the elements’ ends with ‘ks’.

Python3




# import numpy
import numpy as np
 
# Create 1D array of strings.
a = np.array(['hello', 'welcome to',
              'neveropen', 'for', 'neveropen'])
 
# check the strings in an above array
# ends with  substring - 'ks'
# and stored in a variable "gfg".
Data = np.char.endswith(a, 'ks')
 
# Print boolean array
print(Data)


Output:

[False False  True False  True]

Example 2:

In this example, we are creating a NumPy array with 5 strings and checking the element’s ends with ‘o’.

Python3




# import numpy
import numpy as np
 
# Create 1D array of strings.
a = np.array(['hello', 'welcome to',
              'neveropen', 'for', 'neveropen'])
 
# check the strings in an above array
# ends with  substring - 'o'
# and stored in a variable "gfg".
Data = np.char.endswith(a, 'o')
 
# Print boolean array
print(Data)


Output:

[ True  True False False False]

Time complexity: O(N), where N is the number of strings in the array “a”. 

Space complexity : O(N), as we are storing the result in a boolean array “Data” with the same number of elements as the number of strings in the array “a”.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32266 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11864 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS