str.rfind() Reverse find
Last updated: 2026-09-22
str.rfind() Reverse find
str.rfind(sub) returns the highest index where sub is found; -1 if not found.
| Category | String Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
str.rfind(sub[, start[, end]])
⚙️ Parameters
| sub Required | The substring to search for. |
|---|---|
| start Optional | Start index of the range (inclusive); None means the beginning.(Default:None) |
| end Optional | End index of the range (exclusive); None means the end.(Default:None) |
Returns:int. The highest index where sub is found, or -1 if not found.
Mutates the original:No (returns a new object)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
12
-1
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is the difference between rfind() and find()?
Arfind() searches from right to left and returns the position of the last occurrence; when not found it also returns -1 without raising an exception.
QHow do I find the last separator in a path?
AUse rfind(): '/a/b/c.py'.rfind('/') returns the index of the last '/', often used to split at the last occurrence.
QDoes it raise an exception when not found?
ANo, it returns -1. If you want an error when not found, use rindex().