W3Basic Logo

Python String rindex() Method

The str.rindex() method returns the highest index(last occurrence of the substring) in the sequence where the substring is found. If it does not find the substring, it raises a ValueError exception.

Example:

str = "six sick hicks nick six slick bricks with picks and sticks."

# Find the index position of the sub-string "six"
index = str.rindex("six")
print(index)

Output

20

Syntax

The Syntax of the str.rindex() function is:

str.rindex(sub,start,end)

Here, str is a string.

Parameters

The str.rindex() function can take three parameters.

  • sub (required) - The substring that needs to be searched in the string
  • start (optional) - The starting index position where the search should start. The default value is 0.
  • end (optional) - The ending index position where the search should stop. Default is the end of the string or length of the string.

Return Value

The str.rindex() function returns an integer value.

  • if the substring is found in the string, it returns the highest index (last occurrence) where the substring is found.
  • If the substring doesn't exist in the string, it raises ValueError.

Difference Between rindex() method rfind() method

The major difference between rindex() method rfind() method is:

  • The rindex() method raises a ValueError exception if the sub-string is not found in the string, while the rfind() method returns -1.
  • The rindex() method is faster than the rfind() method because it stops searching as soon as it finds the first occurrence of the sub-string.

Example:

str = "the quick brown fox jumps over the lazy dog."

# Find the index position of the sub-string "the" using index()
index = str.rindex("the")
print(index)

# Find the index position of the sub-string "Hi" using index()
try:
    index = str.rindex("Hi")
except ValueError as e:
    print(e)

# Find the index position of the sub-string "the" using find()
index = str.rfind("the")
print(index)

# Find the index position of the sub-string "Hi" using find()
index = str.rfind("Hi")
print(index)

Output

31
substring not found
31
-1

Example 1: How to use rindex() method in Python

The first call to rindex() searches for the substring "fox" starting from index 4. Since "fox" occurs at index 16 in the string, rindex() returns 16.

The second call to rindex() searches for the substring "the" in the entire string. Since "the" occurs in the string at multiple places, the rindex() method returns the index of the last occurrence of the string, which returns 31.

The third call to rindex() searches for the substring "the" from index 0 to index 10 (exclusive). Since "the" occurs at index 0 in the string, rindex() returns 0.

sentence = "the quick brown fox jumps over the lazy dog."

# Search for "fox" starting from index 4
x = sentence.rindex("fox", 4)
print(x)  # Output: 16

# Search for "the" 
x = sentence.rindex("the")
print(x)  # Output: 31

# Search for "the" ending at index 10
x = sentence.rindex("the", 0, 10)
print(x)  # Output: 0

Output

16
31
0

Note: If you want to find the last occurrence of a substring in a string, you can use rindex(). If you want to find the first occurrence, you can use index() method.

Example 2: rindex() method if the substring is not found

The rindex() method raises a ValueError exception if the sub-string is not found in the string.

sentence = "the quick brown fox jumps over the lazy dog."

# Search for "Cat" 
x = sentence.rindex("Cat")
print(x)  

Output

ValueError: substring not found

Example 3: Find the index position of a sub-string in a string using a case-insensitive search:

In this example, we perform a case-insensitive search for the sub-string "zebras" in the string "ZEBRAS zig and ZEBRAS zag". To do this, we first use the lower() method to convert the string and the sub-string to lowercase.

Then we call the rindex() method on the lowercase string, which searches for the first occurrence of the lowercase sub-string. Since "zebras" is found at index positions 0 and 15, the rindex() method returns the last occurrence, i.e., 15.

str = "ZEBRAS zig and ZEBRAS zag"

# Find the index position of the sub-string "zebras" (case-insensitive search)
index = str.lower().rindex("zebras")
print(index)

Output

15

Reference: Python Official Docs

© 2023 W3Basic. All rights reserved.

Follow Us: