Signup/Sign In

Python String rindex()

The Python string rindex() function is used to return the highest index of any substring in the given string. By highest index we mean if a given substring is present twice or thrice in a string then the right most occurence or the last occurence of the substring, that index will be returned by the rindex() method. You can remember it as the right most index.

This is an inbuilt string handling function.

  • If the passed substring that is passed as an argument is not present in the main string, in that case, this function throws an exception.

  • In this function three arguments are passed:

    • the first argument is substring,

    • second and third arguments are starting and ending index of the main string in which we have to search the substring

  • As starting an ending index are optional arguments so if in the case these are not passed then start is taken as 0 while the length is taken as -1.

  • This method is case sensitive, for example it will consider study and Study as two different words.

Python String rindex(): Syntax

Below we have a basic syntax of the string rindex() function in Python:

str.rindex(substr, beginning_index, ending_index)

Note: In the above, str denotes the main string in which we search for the substring.

Python String rindex(): Parameters

The rindex() function accepts 3 parameters and these are given below:

  • substr

    This is used to specify the substring which is a part of the main string whose index we will find using this rindex() function.

  • beginning_index

    This is an optional parameter used to indicate from where the search begins its default value is 0.

  • ending_index

    This is also an optional parameter at this position our search ends.

The beginning_index and ending _index are useful if you want to restrict your search for the substring within a defined part of the string, for example, if you have a string with character lenght 150, and you want to search the substring starting from the 25th character to the 75th character, then you can use the beginning_index and ending_index parameters.

Python String rindex(): Returned Values

If the substring is found inside the main string then its highest index(right most occurence) will be returned. Otherwise, an exception is thrown if the substring is not present inside the string.

Python String rindex(): Basic Example

Below we have an example to show the working of the rindex() method in python:

str1 = "I love StudyTonight"

str2 = "I love Python"
str3 = "Python language doesn't bite"

print("String: ", str1, "Highest Index: ", str1.rindex("love"))

print("String: ", str2, "Highest Index: ", str2.rindex("Python"))
print("String: ", str3, "Highest Index: ", str3.rindex("drink"))

The Output for the above example is given below :


String: I love StudyTonight Highest Index: 2
String: I love Python Highest Index: 7

Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
print("String: ", str3, "Highest Index: ", str3.rindex("drink"))
ValueError: substring not found

Python rindex() and rfind()

Both rindex() and rfind() are similar to each other because both of these methods return the highest index(right most occurence in case of multiple occurence) of the substring from the main string.

But there is a major difference too and that is if they are not able to find the substring rfind() returns, -1 as output whereas the rindex() method throws an exception.

Time for a Live Example!

Let us go through a live example and see the output of code in different situations:

Summary

In this tutorial, we learned that the python rindex() string function is used to return the highest index of the substring that is passed as an argument in this function. Further, we saw its syntax, parameters taken by this function and values returned by this function with a live example.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.