Remember
rindex() returns the rightmost matching substring index. An absent substring raises ValueError.
rindex() returns the rightmost matching substring index. An absent substring raises ValueError.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'banana'
result = text.rindex("a")
print(result)Top-level scope
'banana'Object 2 · int5rindex() returns the rightmost matching substring index. An absent substring raises ValueError.
(no output)
What do you expect this program to print?
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: 5Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'banana'
result = text.rindex("a")
print(result)Top-level scope
'banana'Object 2 · int5The example has finished. Compare the final output with the values and references above.
5
What do you expect this program to print?
text = 'banana'
result = text.rindex("a")
print(result)rindex() MethodThe 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.
string.rindex(substring, start, end)
substring – The string to search forstart – (Optional) The starting index to search fromend – (Optional) The ending index to search to (not inclusive)ValueError if the substring is not found.text = "apple banana apple"
print(text.rindex("apple"))
13
Why? The second "apple" starts at index 13, which is the last occurrence.
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.
text = "hello world"
print(text.rindex("Python"))
ValueError: substring not found
Note: Use rfind() instead if you want to avoid exceptions.
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.
| Method | Returns if Not Found |
|---|---|
rfind() | -1 |
rindex() | Raises ValueError |
rindex() without checking if the substring exists firstrfind() and rindex()rindex() finds the last position of a substring.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")
Connect what you noticed in the example with the idea below.
rindex() returns the rightmost matching substring index. An absent substring raises ValueError.
rindex() returns -1 for an absent substring.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.