Python String rindex() Method

Last Updated : 31 Aug 2026

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 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 Value

It returns either index of substring or throws an exception ValueError.

Examples of Python String rindex() Method

Let's see some examples of rindex() method to understand it's functionality.

Example 1: Finding the Last Occurrence of a Substring

A simple example is created first to see how to implement this method. This method returns index of the substring.

Python

Execute Now

Output:

18

Example 2: Searching Within a Specific Range

This method accept parameters start and end index to search subtring from the substring. See the example below.

Python

Execute Now

Output:

18 
6

Example 3: Handling a Missing Substring

The following example demonstrates that the rindex() method raises a ValueError when the specified substring is not found in the string.

Python

Execute Now

Output:

ValueError: substring not found

Next TopicPython Strings