NumPy strings.index()

The numpy.strings.index() function searches for a substring within a given string or an array of strings. It works similarly to find(), but instead of returning -1 when the substring is not found, it raises a ValueError.

Syntax

</>
Copy
numpy.strings.index(a, sub, start=0, end=None)

Parameters

ParameterTypeDescription
aarray-like (StringDType, bytes_, or str_ dtype)The input string or array of strings to search within.
subarray-like (StringDType, bytes_, or str_ dtype)The substring to find within a.
startarray_like (integer), optionalStarting position of the search. Defaults to 0.
endarray_like (integer), optionalEnding position of the search. Defaults to searching the entire string.

Return Value

Returns an array of integers indicating the index of the first occurrence of the substring in each string. If the substring is not found, it raises a ValueError.


Examples

1. Finding the Index of a Substring

In this example, we find the position of a substring within a given string.

</>
Copy
import numpy as np

# Define a string
word = np.array("apple")

# Find the index of the substring "p"
result = np.strings.index(word, "p")

# Print the result
print("Index of 'p' in 'apple':", result)

Output:

Index of 'p' in 'apple': 1

2. Finding a Substring in an Array of Strings

We use strings.index() to find the position of a substring in multiple words.

</>
Copy
import numpy as np

# Define an array of words
words = np.array(["apple", "banana", "cherry"])

# Find the index of the substring "a" in each word
result = np.strings.index(words, "a")

# Print the results
print("Index of 'a' in each word:", result)

Output:

Since the string 'cherry' does not contain the search string 'a', the index() method throws ValueError.

3. Using Start and End Parameters

Specifying the start and end position to refine the search range.

</>
Copy
import numpy as np

# Define a string
word = np.array("banana")

# Find the index of 'a' after the first occurrence
result = np.strings.index(word, "a", start=2)

# Print the result
print("Index of 'a' after position 2 in 'banana':", result)

Output:

Index of 'a' after position 2 in 'banana': 3

4. Handling a Missing Substring

When the substring is not found, numpy.strings.index() raises a ValueError.

</>
Copy
import numpy as np

# Define a string
word = np.array("cherry")

try:
    # Attempt to find a non-existent substring
    result = np.strings.index(word, "z")
    print("Index of 'z' in 'cherry':", result)
except ValueError:
    print("Substring 'z' not found in 'cherry'.")

Output:

Substring 'z' not found in 'cherry'.

If the substring is not found, a ValueError is raised, which should be handled in a try-except block.

Additional Reading

  1. Python str.index()