Python Reference Library →

Python String rindex() Method – Find Last Occurrence of a Substring

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

  1. Notice

    rindex() returns the rightmost matching substring index. An absent substring raises ValueError.

  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.rindex("a")
print(result)
Live state

Top-level scope

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

rindex() returns the rightmost matching substring index. An absent substring raises ValueError.

Picture string rindex() method – find last occurrence of a substring

rindex() returns the rightmost matching substring index. An absent substring raises ValueError.

Image brief

Alt text
A diagram for Python String rindex() Method – Find Last Occurrence of a Substring. Instructions: text = 'banana' result = text.rindex("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 rindex() Method – Find Last Occurrence of a Substring". Teaching focus: rindex() returns the rightmost matching substring index. An absent substring raises ValueError. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'banana' result = text.rindex("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 rindex() Method – Find Last Occurrence of a Substring. Instructions: text = 'banana' result = text.rindex("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.rindex("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.rindex("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

rindex() returns the rightmost matching substring index. An absent substring raises ValueError.

rindex() returns the rightmost matching substring index. An absent substring raises ValueError.

More examples and reference

Python String rindex() Method

The rindex() method in Python returns the last index of a substring within a string. It works just like rfind(), but instead of returning -1 when not found, it raises a ValueError.

Syntax

string.rindex(substring, start, end)

Parameters:

  • substring – The string to search for
  • start – (Optional) The starting index to search from
  • end – (Optional) The ending index to search to (not inclusive)

Returns:

  • The highest index where the substring is found.

Raises:

  • ValueError if the substring is not found.

Example 1: Basic Usage

text = "apple banana apple"
print(text.rindex("apple"))
13

Why? The second "apple" starts at index 13, which is the last occurrence.

Example 2: With Start and End Index

text = "one two three two one"
print(text.rindex("two", 0, 15))
4

Why? Within the slice from index 0 to 15, the last "two" appears at index 4.

Example 3: Substring Not Found

text = "hello world"
print(text.rindex("Python"))
ValueError: substring not found

Note: Use rfind() instead if you want to avoid exceptions.

Use Case: Reverse Search

You can use rindex() when you want to find the last occurrence of a word or character in a string — such as locating the last period in a sentence, or the last path in a URL.

Difference Between rfind() and rindex()

MethodReturns if Not Found
rfind()-1
rindex()Raises ValueError

Common Mistakes

  • Using rindex() without checking if the substring exists first
  • Mixing up rfind() and rindex()

Summary

  • rindex() finds the last position of a substring.
  • Raises an error if the substring is not found.
  • Can take optional start and end parameters.

Practice Problem

Write a Python program that reads a sentence and prints the last position of the word "end". If not found, print "Not Found".

sentence = input("Enter a sentence: ")
try:
    print("Last index of 'end':", sentence.rindex("end"))
except ValueError:
    print("Not Found")

What to remember

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

Remember

rindex() returns the rightmost matching substring index. An absent substring raises ValueError.

Watch out for this idea

rindex() returns -1 for an absent substring.

This is the misconception to avoid.

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

    Reference library →