Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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 string elements start with a specific prefix, use the numpy.char.startswith() method in NumPy. This function takes the input array as the first parameter and the prefix string as the second parameter.
Syntax
numpy.char.startswith(a, prefix, start=0, end=None)
Parameters
- a − Input array of strings
- prefix − String prefix to check for
- start − Optional start position (default: 0)
- end − Optional end position (default: None)
Basic Example
Let's create a string array and check which elements start with the prefix 'K' −
import numpy as np
# Create a string array
names = np.array(['KATIE', 'JOHN', 'KATE', 'KmY', 'BRAD'])
print("Array:", names)
# Check which names start with 'K'
result = np.char.startswith(names, 'K')
print("Starts with 'K':", result)
Array: ['KATIE' 'JOHN' 'KATE' 'KmY' 'BRAD'] Starts with 'K': [ True False True True False]
Multiple Prefix Examples
You can check for different prefixes on the same array −
import numpy as np
fruits = np.array(['apple', 'banana', 'apricot', 'berry', 'avocado'])
print("Fruits:", fruits)
# Check different prefixes
print("Starts with 'a':", np.char.startswith(fruits, 'a'))
print("Starts with 'b':", np.char.startswith(fruits, 'b'))
print("Starts with 'ap':", np.char.startswith(fruits, 'ap'))
Fruits: ['apple' 'banana' 'apricot' 'berry' 'avocado'] Starts with 'a': [ True False True False True] Starts with 'b': [False True False True False] Starts with 'ap': [ True False True False False]
Using Start and End Parameters
You can specify start and end positions to check prefixes within a substring −
import numpy as np
words = np.array(['hello', 'world', 'help', 'welcome'])
print("Words:", words)
# Check if substring starting at position 1 begins with 'e'
result = np.char.startswith(words, 'e', start=1)
print("Position 1 starts with 'e':", result)
# Check first 3 characters start with 'hel'
result2 = np.char.startswith(words, 'hel', end=3)
print("First 3 chars start with 'hel':", result2)
Words: ['hello' 'world' 'help' 'welcome'] Position 1 starts with 'e': [ True False True True] First 3 chars start with 'hel': [ True False True False]
Practical Use Cases
Filter array elements based on prefix matching −
import numpy as np
emails = np.array(['admin@site.com', 'user@site.com', 'admin@test.com', 'guest@site.com'])
print("Emails:", emails)
# Find admin emails
is_admin = np.char.startswith(emails, 'admin')
admin_emails = emails[is_admin]
print("Admin emails:", admin_emails)
print("Boolean mask:", is_admin)
Emails: ['admin@site.com' 'user@site.com' 'admin@test.com' 'guest@site.com'] Admin emails: ['admin@site.com' 'admin@test.com'] Boolean mask: [ True False True False]
Conclusion
The numpy.char.startswith() function efficiently checks string prefixes and returns a boolean array. Use the boolean result for filtering or conditional operations on string arrays.
Advertisements
