Home / Python / Reference / String methods

Python string rfind() method

Return the position of the last occurrence, or -1.

Syntax

str.rfind(sub)

Searches from the right. Useful for file extensions and paths.

Parameters

subthe text to look for

Return value

int

Example

Code

path = "reports/2026/sales.final.csv"
print(path.rfind("."), path[path.rfind(".") + 1:])
print(path.rfind("/"))

Output

24 csv
12

Related

Learn it step by step in the free lesson Strings, with exercises that check your answers.