
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Return a boolean array where the string element in array ends with a given suffix but test begin and end in Python
To return a boolean array which is True where the string element in array ends with suffix, use the numpy.char.endswith() method in Python Numpy. The method outputs an array of bools. The first parameter is the input array. The second parameter is the suffix. With optional start parameter, test beginning at that position. With optional end parameter, stop comparing at that position.
Steps
At first, import the required library −
import numpy as np
Create a One-Dimensional array of strings −
arr = np.array(['KATIE', 'JOHN', 'KATE', 'AmY', 'CRADlE'])
Displaying our array −
print("Array...\n",arr)
Get the datatype −
print("\nArray datatype...\n",arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nNumber of elements in the Array...\n",arr.size)
To return a boolean array which is True where the string element in array ends with suffix, use the numpy.char.endswith() method −
print("\nResult (endswith)...\n",np.char.endswith(arr, 'E', start = 2, end = 4))
Example
import numpy as np # Create a One-Dimensional array of strings arr = np.array(['KATIE', 'JOHN', 'KATE', 'AmY', 'CRADlE']) # Displaying our array print("Array...\n",arr) # Get the datatype print("\nArray datatype...\n",arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) # Get the shape of the Array print("\nOur Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nNumber of elements in the Array...\n",arr.size) # To return a boolean array which is True where the string element in array ends with suffix, use the numpy.char.endswith() method in Python Numpy # The method outputs an array of bools. print("\nResult (endswith)...\n",np.char.endswith(arr, 'E', start = 2, end = 4))
Output
Array... ['KATIE' 'JOHN' 'KATE' 'AmY' 'CRADlE'] Array datatype... <U6 Array Dimensions... 1 Our Array Shape... (5,) Number of elements in the Array... 5 Result (endswith)... [False False True False False]
- Related Articles
- Return a boolean array where the string element in array begins with a given prefix but test begin and end in Python
- Return a boolean array which is True where the string element in array ends with suffix in Python
- Return a boolean array which is True where the string element in array starts with prefix in Python
- Return the length of a string array element-wise in Python
- How to check if string or a substring of string ends with suffix in Python?
- Return a new array with the same shape as a given array but change the default type in Numpy
- Return an array of zeroes with the same shape as a given array but with a different type in Numpy
- How to check if a string ends with a specified Suffix string in Golang?
- Return a new array of given shape filled with ones but with different datatype in Numpy
- Return a new array with the same shape and type as a given array in Numpy
- Return a full array with the same shape and type as a given array in Numpy
- Return a new array with the same shape and type as given array in Numpy
- AND a given scalar value with every element of a masked array in Python
- Divide a given scalar element with masked array elements and return arrays with Quotient and Remainder in NumPy
- Return the mask of a masked array or full boolean array of False in Numpy

Advertisements