Python String rindex() Method

Returns the index of the last occurrence found of the specified substring. Python will throw a ValueError exception if the substring is not found.

Syntax

Python
string.rindex(value, start, end)

Parameters

ParameterDescription
value Required. The string to search for
start Optional. The index in the string at which to start the search. The deffault is None meaning the start of the string.
end Optional. The index in the string at which to stop the search. The default is None meaning the end of the string.

Example

Python
str = 'Hello World'
print(str.rindex('o'))
try:
    print(str.rindex('q'))
except ValueError:
    print('The string "q" was not found!')

Output

7
The string "q" was not found!