Python String rindex() Method

The rindex() method is used to find the index of the last occurrence of a substring within a string. It raises a ValueError if the substring is not found.

Syntax

string.rindex(substring, start, end)

Parameters

substring: The substring to search for.

start (Optional): The starting index to start the search from. Default is 0.

end (Optional): The ending index to stop the search (exclusive). Default is the end of the string.

Return Value

Returns the index of the last occurrence of a substring within a string. Raises a ValueError if the substring is not found.

Examples

Basic Usage

text = "python is easy, python is awesome"
index = text.rindex("python")
print(index) # Output: 16

Substring Not Found

The rindex() method raises a ValueError if the substring is not found within the string.

text = "python is easy"
index = text.rindex("apple") # Raises ValueError
print(index) 

Handling ValueError

You can easily handle this error using the try-except block.

text = "python is easy"
try:
    index = text.rindex("apple") 
    print(index) 
except ValueError:
    print("Substring not found")

Output:

Substring not found

With start Parameter

You can use the start parameter with the rindex() method if you want to begin searching for a substring from a specific index.

text = "python is easy, python is awesome"
index = text.rindex("python", 17) # Raises ValueError
print(index)

In this example, the rindex() method starts searching for the substring "python" from the index 17 to the end of the string. Since there is no substring "python" after the index 17, it raises ValueError.

With start and end Parameters

You use the start and end parameters with the rindex() method if you want to search for a substring within the specific range of the string.

text = "python is easy, python is awesome"
index = text.rindex("python", 0, 10)
print(index) # Output: 0

In this example, the substring "python" appears twice at indices 0 and 16 of the string text. Since we passed 0 as the start parameter and 10 as the end parameter to the rindex() method, it searches for the last occurrence of the substring "python" within the range from 0 up to (but not including) index 10. Within this range, the substring "python" only appears at the index 0. Therefore rindex() returns 0.

Finding a Single Character

You can also use the rindex() method to get the index of the last occurrence of a single character in a string.

text = "apple"
index = text.rindex("p")
print(index) # Output: 2

Case-Sensitive

The rindex() method is case-sensitive, meaning it treats uppercase and lowercase letters as distinct characters.

text = "Python is awesome"
index = text.rindex("python") # Raises ValueError
print(index) 

In this example, the rindex() method raises ValueError because it is case-sensitive. It looks for the exact lowercase substring "python" but the string text contains "Python" with an uppercase "P". Since the cases don’t match, it raises ValuError.

rindex() VS rfind()

Both rindex() and rfind() methods are used to find the index of the last occurrence of a substring within a string. The key difference is their behavior when a substring is not found:

rindex(): Raises a ValueError

rfind(): Returns -1.