Python String rindex: Complete Guide

Python string function rindex() returns the last index where the substring str is found or raises the exception if no such index exists, optionally restricting the search to string[beg: end].

Python String rindex()

Python rindex() is a built-in string handling method that returns the highest substring index passed in the rindex() function as an argument. If that substring is not present in the main string, the function throws an exception. 

There are three arguments passed, first is the substring, starting and ending indexes of the main string we want to search. To give starting and ending indexes is optional, and if we don’t give it, 0 and length of string -1 are taken as default.

Syntax

string.rindex(value, start, end)

Parameters

The rindex() function takes three arguments.

  1. First is the substring
  2. Second the starting index, 
  3. The third is the ending index.

Return Value

It returns the highest substring index in the main string if that substring is not found; it throws an exception.

Difference between Python rindex() and rfind()

Python rindex() and Python rfind() methods are almost the same; they return the highest index of the substring from the main string. The only difference is that if they cannot find the substring rfind() returns, -1 and rindex() throws an exception.

Example programs on rindex() method in python

Example 1: Write a program to show the working of the rindex() method without using the starting and ending indexes.

# app.py

h1 = "Hello I love AppDividend"
h2 = "AppDividend is the best site for learning"
h3 = "Hello My name is David Letterman"
h4 = "I love Python"
h5 = "Python language doesn't bite"

print("String: ", h1, "Highest Index: ", h1.rindex("love"))
print("String: ", h2, "Highest Index: ", h2.rindex("for"))
print("String: ", h3, "Highest Index: ", h3.rindex("David"))
print("String: ", h4, "Highest Index: ", h4.rindex("Python"))
print("String: ", h5, "Highest Index: ", h5.rindex("drink"))

Output

python3 app.py
String:  Hello I love AppDividend Highest Index:  8
String:  AppDividend is the best site for learning Highest Index:  29
String:  Hello My name is David Letterman Highest Index:  17
String:  I love Python Highest Index:  7
Traceback (most recent call last):
  File "app.py", line 11, in <module>
    print("String: ", h5, "Highest Index: ", h5.rindex("drink"))
ValueError: substring not found

Example 2: Write a program to show the working of the rindex() method using the starting and ending indexes.

# app.py

h1 = "Hello I love AppDividend"
h2 = "AppDividend is the best site for learning"
h3 = "Hello My name is David Letterman"
h4 = "I love Python"
h5 = "Python language doesn't bite"

print("String: ", h1, "Highest Index: ", h1.rindex("love", 3, 14))
print("String: ", h2, "Highest Index: ", h2.rindex("for", 6, 32))
print("String: ", h3, "Highest Index: ", h3.rindex("David", 3, 25))
print("String: ", h4, "Highest Index: ", h4.rindex("Python", 2, 14))
print("String: ", h5, "Highest Index: ", h5.rindex("drink", 3, 16))

Output

python3 app.py
String:  Hello I love AppDividend Highest Index:  8
String:  AppDividend is the best site for learning Highest Index:  29
String:  Hello My name is David Letterman Highest Index:  17
String:  I love Python Highest Index:  7
Traceback (most recent call last):
  File "app.py", line 11, in <module>
    print("String: ", h5, "Highest Index: ", h5.rindex("drink", 3, 16))
ValueError: substring not found

Finally, the Python String rindex() Method Example Tutorial is over.

See also

Python string zfill()

Python string title()

Python string splitlines()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.