NumPy strings.endswith()

The numpy.strings.endswith() function checks if elements in an array of strings end with a specified suffix. It returns a boolean array where each element is True if the corresponding string ends with the given suffix, otherwise False.

Syntax

</>
Copy
numpy.strings.endswith(a, suffix, start=0, end=None)

Parameters

ParameterTypeDescription
aarray-likeInput array containing string elements.
suffixarray-likeSuffix or sequence of suffixes to check against.
startarray_like, optionalPosition in the string where checking begins.
endarray_like, optionalPosition where checking stops.

Return Value

Returns a boolean ndarray of the same shape as the input. Each element is True if the corresponding string ends with the given suffix, otherwise False.


Examples

1. Checking if Strings End with a Specific Suffix

In this example, we check if words in an array end with a given suffix.

</>
Copy
import numpy as np

# Define an array of strings
words = np.array(['apple', 'banana', 'cherry', 'grape'])

# Define the suffix to check
suffix = 'e'

# Check if each word ends with 'e'
result = np.strings.endswith(words, suffix)

# Print the results
print("Words:", words)
print("Ends with 'e':", result)

Output:

Words: ['apple' 'banana' 'cherry' 'grape']
Ends with 'e': [ True False False  True]

2. Checking Multiple Suffixes

We can check if each word ends with any of multiple possible suffixes.

</>
Copy
import numpy as np

# Define an array of strings
words = np.array(['apple', 'banana', 'cherry', 'grape'])

# Define multiple suffixes
suffixes = ('e', 'a')

# Check if each word ends with either 'e' or 'a'
result = np.strings.endswith(words, suffixes)

# Print the results
print("Words:", words)
print("Ends with 'e' or 'a':", result)

Output:

Words: ['apple' 'banana' 'cherry' 'grape']
Ends with 'e' or 'a': [ True  True False  True]

3. Using the start and end Parameters

We can specify a range within each string to check for the suffix.

</>
Copy
import numpy as np

# Define an array of strings
words = np.array(['apple', 'banana', 'cherry', 'grape'])

# Define the suffix and range
suffix = 'p'
start = 1  # Start checking from index 1

# Check if the specified substring ends with 'p'
result = np.strings.endswith(words, suffix, start=start)

# Print the results
print("Words:", words)
print("Ends with 'p' after index 1:", result)

Output:

Words: ['apple' 'banana' 'cherry' 'grape']
Ends with 'p' after index 1: [False False False False]

4. Checking a Suffix with a Specified End Index

We can limit the portion of the string that is checked by specifying an end index.

</>
Copy
import numpy as np

# Define an array of strings
words = np.array(['apple', 'banana', 'cherry', 'grape'])

# Define the suffix and range
suffix = 'n'
end = 4  # Check only up to index 4

# Check if the substring within range ends with 'n'
result = np.strings.endswith(words, suffix, end=end)

# Print the results
print("Words:", words)
print("Ends with 'n' within first 4 characters:", result)

Output:

Words: ['apple' 'banana' 'cherry' 'grape']
Ends with 'n' within first 4 characters: [False False False False]

The function returns False for all elements because no word has 'n' at the end of its first 4 characters.

Additional Reading

  1. Python str.endswith()
  2. Python – Check if String Ends with Specific Suffix