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.
It has the following syntax:
sub: substring to be searched.
start (optional): starting index to start searching.
end (optional): end index where to search stopped.
It returns either index of substring or throws an exception ValueError.
Let's see some examples of rindex() method to understand it's functionality.
A simple example is created first to see how to implement this method. This method returns index of the substring.
Python
Execute NowOutput:
18
This method accept parameters start and end index to search subtring from the substring. See the example below.
Python
Execute NowOutput:
18 6
The following example demonstrates that the rindex() method raises a ValueError when the specified substring is not found in the string.
Python
Execute NowOutput:
ValueError: substring not found
We request you to subscribe our newsletter for upcoming updates.