NumPy | rfind method
rfind(~) method returns the starting index of the last occurrence of the specified substring in each input string. If not found, -1 is returned.
The difference between methods
find(~) is that
rfind(~) returns the index of the last occurrence of the substring, while
find(~) returns the index of the first occurrence. See the example below for clarification.
The source array.
The substring to search for in the source array.
The index to start searching from. By default,
The index to search until. By default, end is equal to the size of the input array.
A Numpy array of integer indices.
np.char.rfind(["aabcd", "def"], "bc")array([ 2, -1])
-1 is returned for
"def" since it does not include the substring
Comparison with find method
Specifying a starting index
np.char.rfind(["abcd"], "ab", start=1)array([-1])
Since we are starting from the 1st index, the search is performed on the string
"bcd", which does not contain the substring
Specifying an ending index
np.char.rfind(["abcd"], "cd", end=3)array([-1])
Since we stop our search at the 3rd index (inclusive), the search is performed on the string
"abc", which does not contain the substring