Python 3 reference · String Methods
Python String rfind() Method
Return the highest index where a substring is found, searching from the right, or -1 if it isn't found.
Python 3 Since 3.0
Syntax
str.rfind(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), or -1.
Examples
path = "/home/ada/notes.txt"
print(path.rfind("/"), path[path.rfind("/") + 1:])
Output
9 notes.txt
print("a.b.c".rfind("."), "abc".rfind("."))
Output
3 -1
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.find()· Return the lowest index where a substring is found, or -1 if it isn't. Optional start and end limit the search.str.rindex()· Return the highest index of a substring, like rfind(), but raise ValueError when it is not found.str.rpartition()· Split the string at the last occurrence of a separator into a 3-tuple: before, separator and after.- Python Strings · tutorial