String Methods

str.rindex()

Locate the last occurrence of a substring entirely within the requested bounds. Return an absolute code-point offset in the original string, not an offset from start. Raise ValueError for a missing match. Empty substrings match boundaries, but a start beyond the original length does not match. The original string is unchanged.

Formal Syntax & Structure

python syntaxpython
text.rindex(sub[, start[, end]])

Parameters & Attributes

NameTypeDescription
substrSubstring to locate or count.
startint | NoneOptional inclusive start bound; negative values are relative to the end.
endint | NoneOptional exclusive end bound; defaults to the end.

Return Value

int

Absolute matching index (raises ValueError if absent).

Working Example

Example Snippetpython
path = "item/item"print(path.rindex("item"))print(path.rindex("item", 1))try:    path.rindex("missing")except ValueError:    print("not found")
Python Reference