NumPy strings.startswith()
The numpy.strings.startswith()
function checks whether each string element in an array starts with a specified prefix. It returns a boolean array indicating which elements start with the given prefix.
Syntax
numpy.strings.startswith(a, prefix, start=0, end=None)
Parameters
Parameter | Type | Description |
---|---|---|
a | array-like (StringDType, bytes_, or str_ dtype) | Input array of strings. |
prefix | array-like (StringDType, bytes_, or str_ dtype) | Prefix to check at the start of each string. |
start | array-like (integer dtype), optional | Specifies the position to start checking within the string. |
end | array-like (integer dtype), optional | Specifies the position to stop checking. |
Return Value
Returns an array of boolean values. Each element is True
if the corresponding string starts with the given prefix, otherwise False
.
Examples
1. Checking If Strings Start with a Given Prefix
Here, we check whether each string in the array starts with the specified prefix.
import numpy as np
# Define an array of strings
strings = np.array(["apple", "banana", "apricot", "blueberry"], dtype="U")
# Define the prefix to check
prefix = "ap"
# Check if each string starts with the prefix
result = np.strings.startswith(strings, prefix)
# Print the result
print("Does each string start with 'ap'?:", result)
Output:
Does each string start with 'ap'?: [ True False True False]

2. Using the start
Parameter
We check whether the substring from a specific position starts with the given prefix.
import numpy as np
# Define an array of strings
strings = np.array(["banana", "bandana", "banish", "banner"], dtype="U")
# Define the prefix to check
prefix = "na"
# Specify the starting position
start_position = 2
# Check if substring starting from index 2 matches the prefix
result = np.strings.startswith(strings, prefix, start=start_position)
# Print the result
print("Does each string start with 'na' from index 2?:", result)
Output:
Does each string start with 'na' from index 2?: [ True False False False]

3. Using the end
Parameter
Here, we limit the substring check within a specified range using the end
parameter.
import numpy as np
# Define an array of strings
strings = np.array(["python", "pyramid", "pythonic", "pyrex"], dtype="U")
# Define the prefix
prefix = "py"
# Specify the range within which to check
start_position = 0
end_position = 3
# Check if strings start with the prefix within the given range
result = np.strings.startswith(strings, prefix, start=start_position, end=end_position)
# Print the result
print("Does each string start with 'py' within the range [0,3]?:", result)
Output:
Does each string start with 'py' within the range [0,3]?: [ True True True True]

4. Checking Multiple Prefixes
We check if each string starts with one of multiple possible prefixes.
import numpy as np
# Define an array of strings
strings = np.array(["hello", "world", "hi", "house"], dtype="U")
# Define an array of prefixes
prefixes = np.array(["he", "wo", "ha", "ho"], dtype="U")
# Check if each string starts with its corresponding prefix
result = np.strings.startswith(strings, prefixes)
# Print the result
print("Does each string start with the corresponding prefix?:", result)
Output:
Does each string start with the corresponding prefix?: [ True True False True]

Each element is checked against its corresponding prefix. The third string "hi"
does not start with "ha"
, so it returns False
.