str.rindex() Reverse find (raises)
Last updated: 2026-09-22
str.rindex() Reverse find (raises)
str.rindex(sub) is like rfind() but raises ValueError when not found.
| Category | String Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
str.rindex(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; raises ValueError if absent.
Mutates the original:No (returns a new object)
💥 Raises
ValueError— Raised when sub is not found in the range.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
3
err: substring not found
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is the difference between rindex() and rfind()?
ABoth search right-to-left for the last occurrence, but when not found rindex() raises ValueError while rfind() returns -1.
QWhen should I use rindex()?
AUse rindex() when the substring is certain to exist (e.g. a marker that must appear in a template), so that not finding it immediately raises an error and exposes the problem; use rfind() when uncertain.
QWhat is the relationship between rindex() and index()?
Aindex() finds the first occurrence, rindex() finds the last; when not found, both raise ValueError.