rindex(str,beg=0,end=len(str))
Python String rindex() Method
Last Updated : 31 Dec 2025
Python rindex() method works same as the rfind() method except it throws error ValueError. This method throws an exception ValueError if the substring is not found. The syntax is given below.
Syntax of Python String rindex() Function
It has the following syntax:
Parameters
sub: substring to be searched.
start (optional): starting index to start searching.
end (optional): end index where to search stopped.
Return
It returns either index of substring or throws an exception ValueError.
Different Examples for Python String rindex() Method
Let's see some examples of rindex() method to understand it's functionality.
Example 1
A simple example is created first to see how to implement this method. This method returns index of the substring.
Output:
Example 2
This method accept parameters start and end index to search subtring from the substring. See the example below.
Output:
Example 3
If the substring is not found in the string, it raises ValueError. See the example below.
Output:
ValueError: substring not found