Python rfind() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

rfind() searches a string from the right and returns the index of the last place where the substring starts. If the substring is not there, it returns -1 and does not raise an error.

The returned index still counts from the left, so "banana".rfind("an") is 3. Optional start and end limit the search to a slice, and the search is case-sensitive. It is the mirror image of find(), which returns the first match.

text.rfind() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Pass the substring to look for. start and end are optional.

python
pos = text.rfind(sub)
pos = text.rfind(sub, start, end)
CallWhat it does
"hello".rfind("l")Returns 3. The last l is at index 3, while the first is at index 2.
"report.final.pdf".rfind(".")Returns 12, the position of the last dot.
"hello".rfind("z")Returns -1. The substring is not in the string.

Use rindex() when a missing substring should raise ValueError. Use find() for the first match, and rpartition() to split at the last separator in one step.

Library declarations
python
str.rfind(sub[, start[, end]]) -> int
# returns: the highest index where sub starts, or -1 if it is missing
# raises: TypeError if sub is not a string

Parameters

ParameterDescription
subRequired. The substring to look for. It must be a string, and it can be longer than one character.
startOptional. Index where the search area begins. Default 0.
endOptional. Index where the search area stops, exclusive. Default is the end of the string.

Return Value

Return ValueDescription
intThe index of the last match, counted from the left, or -1 if there is no match.

Python Examples

Example 1 — Find the Last Occurrence

Compare find() and rfind() on the same text, and try a missing substring.

python
text = "banana"

print(text.find("an"))
print(text.rfind("an"))
print(text.rfind("a"))
print(text.rfind("x"))

How It Works

  1. find() starts at the left. The first "an" begins at index 1.
  2. rfind() starts at the right. The last "an" begins at index 3, so the two methods give different answers.
  3. One letter. The last "a" is the final character, at index 5.
  4. No match. "x" is not in the text, so the result is -1.

Example 2 — Separate a Folder From a File Name

Use the last slash and the last dot to cut a path into folder, name, and extension.

python
path = "/home/ada/notes.txt"

slash = path.rfind("/")
folder = path[:slash]
filename = path[slash + 1:]

dot = filename.rfind(".")
print(slash, dot)
print(folder, filename[:dot], filename[dot + 1:])

How It Works

  1. The last slash. rfind("/") returns 9, which is the slash before notes.txt, not the first one.
  2. Slice around it. path[:slash] is the folder, and path[slash + 1:] is everything after the slash.
  3. The last dot. In notes.txt, the dot is at index 5, so the name is notes and the extension is txt.

Example 3 — Search Only Part of the Text

Use start and end to look at a slice without copying it.

python
text = "one two one two"

print(text.rfind("one"))
print(text.rfind("one", 0, 7))
print(text.rfind("two", 0, 10))
print(text.rfind("two", 0, 5))

How It Works

  1. Whole string. The last "one" begins at index 8.
  2. Stop earlier. With end set to 7, only "one two" is searched, so the last "one" is the one at index 0.
  3. A different word. Inside the first ten characters, the last "two" starts at index 4.
  4. Too short a window. The first five characters are "one t", which does not contain a full "two", so the result is -1.

Frequently Asked Questions

It returns the index of the last occurrence of a substring in a string, or -1 if the substring is not found.
find() returns the first match and rfind() returns the last match. Both return -1 when the substring is missing.
They find the same position, but when the substring is missing rfind() returns -1 while rindex() raises ValueError.
No. The search runs from the right, but the index it returns is still counted from the left, starting at 0.
Use text[text.rfind(sep) + 1:], or use rpartition(sep) and take the last item, which also works when the separator is missing.

Next: rindex()

Return the index of the last match of a substring, or raise ValueError if it is missing.

rindex() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful