Python String rindex()

The Python String rindex() method is a built-in function that returns the substring’s highest index (last occurrence) in a given string. If not found, it raises ValueError: substring not found exception.

In this article, we will learn about the Python string rindex() method with the help of examples.

rindex() Syntax

The Syntax of rindex() method is:

str.rindex(sub, start, end)

rindex() Parameters

The rindex() method can take three parameters.

  • sub – substring that needs to be searched in the given string.
  • start (optional) – starting position where the substring needs to be searched within the string
  • end (optional) – ending position where the substring needs to be searched within the string
Note: If the start and end indexes are not provided, by default, it takes 0 as the starting index and str.length-1 as the end index

rindex() Return Value

The rindex() method returns an integer value, an index of a substring.

  • If the substring is found in the given string, the rindex() method will return the highest index of the substring.
  • If the substring is not found in the given string, it throws ValueError: substring not found exception

Difference between rindex() method and rfind() method

The rindex() method is similar to rfind() method. The only major difference is that the rfind() method returns -1 if the substring is not found in a given string, whereas the rindex() method will raise the ValueError: substring not found exception.

Example 1: Find the last occurence of a string in Python

The index in Python starts from 0 and not from 1. Hence in the below example the last occurence of the substring ‘Code‘ is found at index position 25.

text = 'ItsMyCode - Learn how to Code in Python '

# find the index of last  occurence of substring
output = text.rindex('Code')
print("The index of last  occurrence of 'Code' is:", output)

# find the index of character
output = text.rindex('-')
print("The index of last occurrence of '-' is:", output)

Output

The index of last  occurrence of 'Code' is: 25
The index of last occurrence of '-' is: 10

Example 2: If string is not found ValueError: substring not found exception

Here the substring ‘Hello’ is not found in the given string and hence Python interpreter raises ValueError: substring not found exception.

text = 'ItsMyCode - Learn how to Code in Python '

# find the index of last occurence of substring
output = text.index('Hello')
print("The index of last occurrence of 'Hello' is:", output)

Output

Traceback (most recent call last):
  File "C:\Personal\IJS\Code\main.py", line 4, in <module>
    output = text.index('Hello')
ValueError: substring not found

Example 3: Python String rindex() using start and end arguments

text = 'ItsMycode - code, coder, coders, coding  '

# find the index of last occurence of substring 'code'
output = text.rindex('code', 18)
print("The index of last occurrence of 'code' is:", output)

# find the index of character ','
output = text.rindex(',', 8, 20)
print("The index of last occurrence of ',' is:", output)

# find the index of substring 'de'
output = text.rindex('de', 10, -6)
print("The index of last occurrence of 'de' is:", output)

Output

The index of last occurrence of 'code' is: 25
The index of last occurrence of ',' is: 16
The index of last occurrence of 'de' is: 27
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Dir()

Python dir()

Table of Contents Hide dir() Syntax dir() Parametersdir() Return ValueExample 1: How dir() works?Example 2: When no parameters are passed to dir() method with and without importing external libraries.Example 3: When a module…
View Post
Python All()

Python all()

Table of Contents Hide all() Syntaxall() Parametersall() Return ValueDifference between any() and all() functions in PythonExample 1 – Using all() function on Python ListsExample 2 – Using all() function on…
View Post
Python String Startswith()

Python String startswith()

Table of Contents Hide startswith() Syntaxstartswith() Parameterstartswith() Return ValueExample 1: startswith() Without start and end ParametersExample 2: startswith() With start and end ParametersPassing Tuple to startswith() Python String startswith() method…
View Post
Python String Endswith()

Python String endswith()

Table of Contents Hide endswith() Syntaxendswith() Parameterendswith() Return ValueExample 1: endswith() Without start and end ParametersExample 2: endswith() With start and end ParametersPassing Tuple to endswith() Python String endswith() method…
View Post