NumPy strings.rfind()

The numpy.strings.rfind() function returns the highest index in a string where a specified substring is found, within an optional range. If the substring is not found, it returns -1.

Syntax

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

Parameters

ParameterTypeDescription
aarray-like (StringDType, bytes_, or str_ dtype)The input string or array of strings.
subarray-like (StringDType, bytes_, or str_ dtype)The substring to search for.
startarray_like (integer dtype, optional)The starting index of the range where the search is performed. Defaults to 0.
endarray_like (integer dtype, optional)The ending index (exclusive) of the range where the search is performed. Defaults to searching until the end of the string.

Return Value

Returns an array of integers representing the highest index where the substring is found. If the substring is not found, it returns -1.


Examples

1. Finding the Last Occurrence of a Substring

Here, we search for the highest index where a substring appears in a given string.

</>
Copy
import numpy as np

# Define the input string
string = np.array("hello world, welcome to the world of NumPy")

# Define the substring to search for
substring = np.array("world")

# Find the last occurrence of the substring
result = np.strings.rfind(string, substring)

# Print the result
print("Last occurrence index of 'world':", result)

Output:

Last occurrence index of 'world': 24

The substring "world" appears twice, and the highest index where it appears is at position 24.

2. Searching Within a Range

We can limit the search within a specific range using the start and end parameters.

</>
Copy
import numpy as np

# Define the input string
string = np.array("hello world, welcome to the world of NumPy")

# Define the substring to search for
substring = np.array("world")

# Define the search range (start at index 0, end at index 20)
result = np.strings.rfind(string, substring, start=0, end=20)

# Print the result
print("Last occurrence index of 'world' within range [0, 20]:", result)

Output:

Last occurrence index of 'world' within range [0, 20]: 6

Since we restricted the search range to [0, 20], the function returns 6, which is the index of the first occurrence of "world".

3. Handling a Substring That Does Not Exist

If the substring is not found in the input string, the function returns -1.

</>
Copy
import numpy as np

# Define the input string
string = np.array("hello world, welcome to NumPy")

# Define a substring that does not exist in the string
substring = np.array("Python")

# Try to find the last occurrence of the substring
result = np.strings.rfind(string, substring)

# Print the result
print("Index of non-existing substring 'Python':", result)

Output:

Index of non-existing substring 'Python': -1

Since the substring "Python" does not appear in the string, the function returns -1.

4. Finding Substring Indices in an Array of Strings

We can use numpy.strings.rfind() on an array of strings to find the last occurrence of a substring in each element.

</>
Copy
import numpy as np

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

# Define the substring to search for
substring = np.array("banana")

# Find the last occurrence of 'banana' in each string
result = np.strings.rfind(strings, substring)

# Print the results
print("Last occurrence indices of 'banana' in each string:", result)

Output:

Last occurrence indices of 'banana' in each string: [6 14 7]

The function finds the last occurrence of "banana" in each string within the array and returns their respective indices.