What is the numpy.char.endswith() function from NumPy in Python?
char.endswith(a, suffix, start=0, end=None)
Syntax for the char.endswith() function
Parameter value
The char.endswith() function takes the following parameter values:
a: This is the input array.suffix: This is the character string from the input array to be checked for.
start: With an optionalstart, the test to test begins at that position.end: With an optionalend, the comparison stops at the given position.
Return value
The char.endswith() function returns an output array having Boolean values.
Example
import numpy as np# creating an input arraya = np.array(["Hello", "Red", "Goat", "Tired"])# implementing the char.endwith() function to check if the elements ends with "ed"myarray = np.char.endswith(a, "ed", start=1, end=3 )# printing the resultprint(myarray)
Explanation
- Line 1: We import the
numpymodule. - Line 3: We create an input array
ausing thearray()function. - Line 6: We use the
char.endswith()function to check if the string characteredappears in each element of the input arraya. We start from the second character (index 1)to the third character (index 2) of each element. We assign the result to a variable,myarray. - Line 9: We print the variable
myarray.