NumPy strings.find()
The numpy.strings.find()
function searches for a substring within each element of an input string array and returns the lowest index where the substring is found. If the substring is not found, it returns -1
. This function is useful for locating specific text within a collection of strings.
Syntax
numpy.strings.find(a, sub, start=0, end=None)
Parameters
Parameter | Type | Description |
---|---|---|
a | array_like | Input string array where the search is performed. |
sub | array_like | The substring to search for within each element. |
start | array_like, optional | The starting index from where to begin searching. Default is 0 . |
end | array_like, optional | The ending index (exclusive) where the search should stop. Default is the length of the string. |
Return Value
Returns an array of integers representing the lowest index where the substring is found in each input string. If the substring is not found, it returns -1
.
Examples
1. Finding Substring in an Array of Strings
Here, we search for a substring in a list of fruit names.
import numpy as np
# Define an array of strings
fruits = np.array(["apple", "banana", "cherry", "blueberry"])
# Search for the substring 'an' in each string
result = np.strings.find(fruits, "an")
# Print the results
print("Index positions of 'an':", result)
Output:
Index positions of 'an': [-1 1 -1 -1]

The substring 'an'
is found at index 1
in "banana"
. In other words, it starts from the second character. The other words do not contain 'an'
, so the result is -1
for them.
2. Specifying a Start Index
We define a starting index to begin searching for the substring.
import numpy as np
# Define an array of strings
fruits = np.array(["apple", "banana", "cherry", "blueberry"])
# Search for 'e' starting from index 3
result = np.strings.find(fruits, "e", start=3)
# Print the results
print("Index positions of 'e' from index 3:", result)
Output:
Index positions of 'e' from index 3: [-1 -1 4 6]

The search starts at index 3
for each word. For example, in "cherry"
, "e"
is found at index 4
. If "e"
appears before index 3
, it is ignored.
3. Specifying a Start and End Index
Here, we limit the search within a specific range.
import numpy as np
# Define an array of strings
fruits = np.array(["apple", "banana", "cherry", "blueberry"])
# Search for 'r' within a limited index range (2 to 5)
result = np.strings.find(fruits, "r", start=2, end=5)
# Print the results
print("Index positions of 'r' from index 2 to 5:", result)
Output:
Index positions of 'r' from index 2 to 5: [-1 -1 3 -1]

The letter 'r'
is found in "cherry"
at index 3
, which is within the specified range (2 to 5). For other words, it either appears outside the range or does not exist.
4. Searching for a Non-Existent Substring
If the substring does not exist in the given string, the function returns -1
.
import numpy as np
# Define an array of strings
fruits = np.array(["apple", "banana", "cherry", "blueberry"])
# Search for a substring that does not exist
result = np.strings.find(fruits, "z")
# Print the results
print("Index positions of 'z':", result)
Output:
Index positions of 'z': [-1 -1 -1 -1]

Since 'z'
does not appear in any of the words, the function returns -1
for all elements.