Python 3 reference · String Methods

Python String rfind() Method

Return the highest index where a substring is found, searching from the right, or -1 if it isn't found.

Python 3 Since 3.0

Syntax

str.rfind(sub, start=0, end=len(s))

Parameters

ParameterDescription
sub The substring to look for.
start, end optional Only search this slice.

Return value

The index (an int), or -1.

Examples

Find the last slashTry it yourself
path = "/home/ada/notes.txt"
print(path.rfind("/"), path[path.rfind("/") + 1:])

Output

9 notes.txt
Last occurrence or -1Try it yourself
print("a.b.c".rfind("."), "abc".rfind("."))

Output

3 -1

Version history

VersionChange
Python 3.0Added