Python Glossary Русский

all Python 3Strings (str)function

str.rindex

search

Like rfind, but raise ValueError when the substring is not found.

Syntax

s.rindex(sub[, start[, end]])

The only difference from rfind is the failure mode: rfind returns -1, rindex raises ValueError. Reach for rindex when a missing substring means broken data and you want the crash; if you are going to inspect the result anyway, rfind reads better than a try/except. The start and end arguments narrow the search window, but the index you get back is still counted from the beginning of the whole string.

Examples

print('abcabc'.rindex('a'))  # → 3
print('abcabc'.rindex('b'))  # → 4
print('hello'.rindex('l'))   # → 3

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy