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

To return a boolean array which is True where the string element in array ends with a specific suffix, use the numpy.char.endswith() method. This function performs vectorized string operations on NumPy string arrays, checking each element for the specified suffix.

Syntax

numpy.char.endswith(a, suffix, start=0, end=None)

Parameters

a ? Input array of strings

suffix ? String suffix to check for

start ? (Optional) Start position for checking

end ? (Optional) End position for checking

Basic Example

Let's create a string array and check which elements end with a specific suffix ?

import numpy as np

# Create array of strings
names = np.array(['KATIE', 'JOHN', 'KATE', 'AmY', 'BRADley'])

print("Array:")
print(names)

# Check which names end with 'E'
result = np.char.endswith(names, 'E')
print("\nElements ending with 'E':")
print(result)
Array:
['KATIE' 'JOHN' 'KATE' 'AmY' 'BRADley']

Elements ending with 'E':
[ True False  True False False]

Multiple Suffix Examples

You can check for different suffixes and combine results ?

import numpy as np

words = np.array(['running', 'walked', 'jumping', 'played', 'swimming'])

# Check for different suffixes
ends_with_ing = np.char.endswith(words, 'ing')
ends_with_ed = np.char.endswith(words, 'ed')

print("Words:", words)
print("Ends with 'ing':", ends_with_ing)
print("Ends with 'ed':", ends_with_ed)

# Words ending with either suffix
either_suffix = ends_with_ing | ends_with_ed
print("Ends with 'ing' OR 'ed':", either_suffix)
Words: ['running' 'walked' 'jumping' 'played' 'swimming']
Ends with 'ing': [ True False  True False  True]
Ends with 'ed': [False  True False  True False]
Ends with 'ing' OR 'ed': [ True  True  True  True  True]

Case-Sensitive Checking

The endswith() method is case-sensitive. Here's how to handle different cases ?

import numpy as np

mixed_case = np.array(['Apple', 'BANANA', 'orange', 'Grape'])

# Case-sensitive check
print("Original array:", mixed_case)
print("Ends with 'e':", np.char.endswith(mixed_case, 'e'))
print("Ends with 'E':", np.char.endswith(mixed_case, 'E'))

# Convert to lowercase first for case-insensitive check
lowercase_array = np.char.lower(mixed_case)
print("Case-insensitive check for 'e':", np.char.endswith(lowercase_array, 'e'))
Original array: ['Apple' 'BANANA' 'orange' 'Grape']
Ends with 'e': [ True False  True  True]
Ends with 'E': [False False False False]
Case-insensitive check for 'e': [ True False  True  True]

Practical Use Case

Filter array elements based on suffix matching ?

import numpy as np

files = np.array(['document.pdf', 'image.jpg', 'data.csv', 'photo.png', 'report.pdf'])

# Find all PDF files
is_pdf = np.char.endswith(files, '.pdf')
pdf_files = files[is_pdf]

print("All files:", files)
print("PDF files only:", pdf_files)
print("Number of PDF files:", np.sum(is_pdf))
All files: ['document.pdf' 'image.jpg' 'data.csv' 'photo.png' 'report.pdf']
PDF files only: ['document.pdf' 'report.pdf']
Number of PDF files: 2

Conclusion

The numpy.char.endswith() method efficiently checks string suffixes across entire arrays, returning boolean arrays for filtering or conditional operations. It's particularly useful for file processing and text analysis tasks.

Updated on: 2026-03-26T19:57:44+05:30

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements