Python 3 reference · String Methods

Python String rindex() Method

Return the highest index of a substring, like rfind(), but raise ValueError when it is not found.

Python 3 Since 3.0

Syntax

str.rindex(sub, start=0, end=len(s))

Parameters

ParameterDescription
sub The substring to look for.
start, end optional Only search this slice.

Return value

The index (an int).

Examples

Last occurrenceTry it yourself
print("hello world".rindex("o"))

Output

7
Missing substring raisesTry it yourself
try:
    "abc".rindex("z")
except ValueError:
    print("not found")

Output

not found

Version history

VersionChange
Python 3.0Added