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
| Parameter | Description |
|---|---|
sub |
The substring to look for. |
start, end optional |
Only search this slice. |
Return value
The index (an int).
Examples
print("hello world".rindex("o"))
Output
7
try:
"abc".rindex("z")
except ValueError:
print("not found")
Output
not found
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.rfind()· Return the highest index where a substring is found, searching from the right, or -1 if it isn't found.str.index()· Return the lowest index of a substring, like find(), but raise ValueError when it is not found.- Python Strings · tutorial