Home » Python String | rindex() method with Examples

Python String | rindex() method with Examples

by Online Tutorials Library

Python String rindex() Method

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.

Signature

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.

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

Python String rindex() Method Example : Simple Example

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

Output:

18  

Python String rindex() Method Example 2

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

Output:

18   6  

Python String rindex() Method Example 3

If the substring is not found in the string, it raises ValueError. See the example below.

Output:

ValueError: substring not found  

Next TopicPython Strings

You may also like