
- 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 which is True where the string element in array starts with prefix in Python
To return a boolean array which is True where the string element in array starts with prefix, use the numpy.char.startswith() method in Python Numpy. The first parameter is the input array. The second parameter is the prefix.
Steps
At first, import the required libraries −
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 starts with prefix, use the numpy.char.startswith() method −
print("\nResult (startswith)...\n",np.char.startswith(arr, 'K'))
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 starts with prefix, use the numpy.char.startswith() method in Python Numpy # The first parameter is the input array # The second parameter is the prefix print("\nResult (startswith)...\n",np.char.startswith(arr, 'K'))
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]
- Related Articles
- Return a boolean array which is True where the string element in array ends with suffix in Python
- 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 where the string element in array ends with a given suffix but test begin and end in Python
- Return the length of a string array element-wise in Python
- Return TRUE if the first string starts with a specific second string JavaScript
- JavaScript Determine the array having majority element and return TRUE if its in the same array
- How to check if a string starts with a specified Prefix string in Golang?
- Return the mask of a masked array or full boolean array of False in Numpy
- For each element return the lowest index in the string where substring is found in Python
- For each element return the highest index in the string where substring is found in Python
- How can I update all elements in an array with a prefix string?
- For each element return the highest index in the string where substring is found in a range in Python
- For each element return the lowest index in the string where substring is found in a range in Python
- Return the element-wise square of the array input in Python
- Python Pandas - Return whether any element in the index is True

Advertisements