Python 3 – String rindex() Method
The rindex()
method returns the highest index of the substring (if found in the given string) which is passed as a parameter in this method. It searches for the substring, passed as a parameter, from the end of the given string and if it is found, returns the highest (the last) index of the substring in the given string.
The rindex()
method is similar to the index()
method, but instead of searching from the start of the string, it searches from the end of the string.
Syntax
string.rindex(substring, start, end)
Parameters
The rindex()
method takes three parameters:
substring
(mandatory): A substring to search for in the given string.start
(optional): The starting index of the search within the string. The default value is 0.end
(optional): The ending index of the search within the string. The default value is the length of the string.
Return Value
The rindex()
method returns the highest index of the substring (if found) in the given string. It raises a ValueError exception if the specified substring is not found.
Examples
Let’s take a look at some examples to understand the rindex()
method better:
Example 1: Using the rindex()
Method Without Specifying the Start and End Positions
# Python 3 code to demonstrate the use of `rindex()` method without specifying the start and end positions
# Initial string
string = "Hello, world! Welcome to Python tutorials"
# Printing the original string
print("Original string: " + string)
# Using `rindex()` method to find the index of the last occurrence of the character 'o'
index = string.rindex('o')
# Printing the index of the last occurrence of the character 'o'
print("The last occurrence of 'o' is at index:", index)
Output:
Original string: Hello, world! Welcome to Python tutorials
The last occurrence of 'o' is at index: 35
In this example, we have a string, Hello, world! Welcome to Python tutorials
. We use the rindex()
method to find the index of the last occurrence of the character ‘o’. Since we didn’t specify the start and end positions, the rindex()
method starts searching from the end of the string and goes up to the beginning.
Example 2: Using the rindex()
Method with Specifying the Start and End Positions
# Python 3 code to demonstrate the use of `rindex()` method with specifying the start and end positions
# Initial string
string = "Hello, world! Welcome to Python tutorials"
# Printing the original string
print("Original string: " + string)
# Using `rindex()` method to find the index of the last occurrence of the character 'o' within specified start and end positions
index = string.rindex('o', 0, 10)
# Printing the index of the last occurrence of the character 'o'
print("The last occurrence of 'o' within specified start and end positions is at index:", index)
Output:
Original string: Hello, world! Welcome to Python tutorials
The last occurrence of 'o' within specified start and end positions is at index: 4
In this example, we have a string, Hello, world! Welcome to Python tutorials
. We use the rindex()
method to find the index of the last occurrence of the character ‘o’ within the specified start and end positions (0 and 10, respectively). The rindex()
method starts searching from index 10 and goes up to index 0. Since there is no occurrence of the character ‘o’ between these positions, it raises a ValueError exception.
Example 3: Using the rindex()
Method with Incorrect Substring
# Python 3 code to demonstrate the use of `rindex()` method with incorrect substring
# Initial string
string = "Hello, world! Welcome to Python tutorials"
# Printing the original string
print("Original string: " + string)
# Using `rindex()` method to find the index of the last occurrence of the character 'z'
try:
index = string.rindex('z')
print("The last occurrence of 'z' is at index:", index)
except ValueError as e:
print(e)
Output:
Original string: Hello, world! Welcome to Python tutorials
substring not found
In this example, we have a string, Hello, world! Welcome to Python tutorials
. We use the rindex()
method to find the index of the last occurrence of the character ‘z’. Since there is no occurrence of the character ‘z’ in the given string, the rindex()
method raises a ValueError exception with the message ‘substring not found’.
Conclusion
In this article, we learned about the rindex()
method in Python 3. The rindex()
method is used to find the index of the last occurrence of a substring in a string. It searches for the substring from the end of the string and if it is found, returns the highest index of the substring in the given string. It takes three parameters, of which the substring is mandatory, and the start and end positions are optional. If the substring is not found, it raises a ValueError exception.