Skip to content

Python String rindex() Method

How to use Python's rindex() method to find the last occurrence of a substring in a string, including parameter details and handling ValueError exceptions.

The String rindex() method searches for the last occurrence of the given string and returns its index. If specified string is not found, it raises a ValueError exception.

Syntax

my_string.rindex(sub, start, end)

rindex() Parameters

Python String rindex() method parameters:

Parameter Condition Description
sub Required Any string you want to search for
start Optional An index specifying where to start the search. Default value is 0.
end Optional An index specifying where to stop the search. Default value is the end of the string.

rindex() Return Value

Python String rindex() method returns an integer value corresponding to the last occurrence of the substring, if the substring exists inside the string.

Examples

Example 1: Get index of given String in a String

If the given string is contained in the string, then rindex() method returns the index of the last occurrence:

my_str = 'Tom is a Developer on tutorialreference.com Development Team'
result = my_str.rindex('Dev')
print(result) # Output: 44

output

44

If the given string is not found in the string, then rindex() method raises ValueError exception.

my_str = 'Tom is a Developer on tutorialreference.com Development Team'
result = my_str.index('Ryan') # Raises ValueError: substring not found

output

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    result = my_str.index('Ryan')
ValueError: substring not found

Example 2: Get index of given String in a String with start and end indexes

If you want to limit the search to a substring of the given string, you can specify the start parameter and/or the end parameter.

my_str = 'Tom is a Developer on tutorialreference.com Development Team'
sub = "Dev"

result = my_str.rindex(sub)       
print(result) # Output: 44

result = my_str.rindex(sub, 4)
print(result) # Output: 44

result = my_str.rindex(sub, 4, 6) 
print(result) # Raises ValueError: substring not found

output

44
44
Traceback (most recent call last):
  File "main.py", line 13, in <module>
    result = my_str.rindex(sub, 4, 6) 
ValueError: substring not found

Example 3: Handling ValueError of rindex() method

You can handle the ValueError exception using a try-except block.

my_str = "Python is powerful."
sub = "Java"
try:
    index = my_str.rindex(sub)
    print("The index where the substring is found:", index)
except ValueError:
    print("The substring does not exist in the text.")

output

The substring does not exist in the text.

rindex() method vs rfind() method

The rindex() method is identical to the rfind() method.

The only difference is that the rfind() method returns -1 (instead of raising a ValueError exception), if the substring is not found.

An example to compare rfind() and rindex():

my_str = 'Tom is a Developer at ABC'
result = my_str.rfind('Manager')
print(result) # Output: -1

my_str = 'Tom is a Developer at ABC'
result = my_str.rindex('Manager') # Triggers ValueError: substring not found

output

-1
Traceback (most recent call last):
  File "main.py", line 6, in <module>
    result = my_str.rindex('Manager')
ValueError: substring not found
Navigation