To return a boolean array which is True where the string element in array begins with prefix, use the numpy.char.startswith() method in Python Numpy. The method outputs an array of bools. The first parameter is the input array. The second parameter is the prefix. 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', 'KmY', 'BRAD'])
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 begins with prefix, use the numpy.char.startswith() method. The method outputs an array of bools −
print("\nResult (startswith)...\n",np.char.startswith(arr, 'K', start = 0, end = 2))
Example
import numpy as np # Create a One-Dimensional array of strings arr = np.array(['KATIE', 'JOHN', 'KATE', 'KmY', 'BRAD']) # 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 begins with prefix, use the numpy.char.startswith() method in Python Numpy # The method outputs an array of bools. print("\nResult (startswith)...\n",np.char.startswith(arr, 'K', start = 0, end = 2))
Output
Array... ['KATIE' 'JOHN' 'KATE' 'KmY' 'BRAD'] Array datatype... <U5 Array Dimensions... 1 Our Array Shape... (5,) Number of elements in the Array... 5 Result (startswith)... [ True False True True False]