Python Reference Library →

Python String rfind() Method – Find Last Occurrence of Substring

Let’s look at string rfind() method – find last occurrence of substring

  1. Notice

    rfind() returns the rightmost matching substring index or -1 when the substring is absent.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'banana'
result = text.rfind("a")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'banana'Object 2 · int5
This step

rfind() returns the rightmost matching substring index or -1 when the substring is absent.

Picture string rfind() method – find last occurrence of substring

rfind() returns the rightmost matching substring index or -1 when the substring is absent.

Image brief

Alt text
A diagram for Python String rfind() Method – Find Last Occurrence of Substring. Instructions: text = 'banana' result = text.rfind("a") print(result). Displayed result: 5. The final example bindings are text --> object 1 (str); result --> object 2 (int). object 1: 'banana'; object 2: 5
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String rfind() Method – Find Last Occurrence of Substring". Teaching focus: rfind() returns the rightmost matching substring index or -1 when the substring is absent. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'banana' result = text.rfind("a") print(result) | v DISPLAYED RESULT 5 FINAL BINDINGS text --> object 1 (str) result --> object 2 (int) object 1: 'banana' object 2: 5 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String rfind() Method – Find Last Occurrence of Substring. Instructions: text = 'banana' result = text.rfind("a") print(result). Displayed result: 5. The final example bindings are text --> object 1 (str); result --> object 2 (int). object 1: 'banana'; object 2: 5

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'banana'
result = text.rfind("a")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'banana'Object 2 · int5
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
5

What do you expect this program to print?

What does this example display?

text = 'banana'
result = text.rfind("a")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

5

Follow the printed values in order. The output is: 5

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

rfind() returns the rightmost matching substring index or -1 when the substring is absent.

rfind() returns the rightmost matching substring index or -1 when the substring is absent.

More examples and reference

Python String rfind() Method

The rfind() method in Python is used to find the last occurrence of a substring in a given string. It returns the highest index where the substring is found, or -1 if it's not found.

Syntax

string.rfind(substring, start, end)

Parameters:

  • substring – The string to search for.
  • start (optional) – The starting index to begin search.
  • end (optional) – The ending index where search stops.

Returns:

  • Index (int) of the last occurrence of the substring.
  • -1 if the substring is not found.

Example 1: Basic Usage of rfind()

text = "hello world, welcome to the world of Python"
index = text.rfind("world")
print(index)
28

Why? The substring "world" occurs twice, and the last one starts at index 28.

Example 2: Substring Not Found

text = "python programming"
print(text.rfind("java"))
-1

Why? Because "java" is not found in the string.

Example 3: Using Start and End Parameters

text = "abc abc abc"
print(text.rfind("abc", 0, 7))
4

Why? It searches only from index 0 to 7. The last "abc" in this range starts at index 4.

Difference Between find() and rfind()

  • find() returns the first occurrence
  • rfind() returns the last occurrence

Use Cases of rfind()

  • Finding the position of the last slash (/) in a file path
  • Getting the extension from the last dot in a filename
  • Parsing or slicing strings based on the last match

Common Mistakes

  • Confusing rfind() with rindex() – rindex() raises an error if substring not found, rfind() returns -1.
  • Ignoring optional start and end which can restrict search range.

Practice Problem

Given a string containing multiple email addresses separated by commas, find the last occurrence of “@gmail.com”.

emails = "john@yahoo.com, jane@gmail.com, doe@gmail.com, someone@outlook.com"
print(emails.rfind("@gmail.com"))

Expected Output:

38

Summary

  • rfind() returns the highest index of a substring
  • Returns -1 if the substring is not found
  • Useful when you need to search from the end of the string

Next Steps

Practice with strings that have multiple repeating words. Try restricting the search range with start and end parameters for deeper understanding.

What to remember

Connect what you noticed in the example with the idea below.

Remember

rfind() returns the rightmost matching substring index or -1 when the substring is absent.

Watch out for this idea

rfind() returns an index counted backward from the end.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →