In this tutorial, we’ll learn what Python string rindex() method is and how to properly use it. We’ll go through multiple Python code examples to understand how rindex method works.
Introduction: Python String rindex() Method
This method returns the highest index value of specified character/substring. It raises an exception if the substring is not found.
Syntax of rindex() Method
string.rindex(substring, startingPoint(optional), endingPoint(optional))
- Python string rindex method can take up to three parameters.
- First parameter is the character/substring whose highest index value is to be searched.
- Second parameter is used to specify the starting point from where the search will start.
- Third parameter is used to specify the ending point of search. These two parameters and actually used to limit the search range. The whole string will be searched if no search limit is specified.
- This method raises an exception if specified substring is not found.
- Python string rindex method is case sensitive so a and A will be taken/used as different values. Same goes for other alphabets as well.
Example 1: rindex() Method applied on a Python String
print('Python Programming'.rindex('P')) print('Python Programming Python language'.rindex('Python')) print('Python Programming'.rindex('p')) // raises an exception as p(not capital) is not present in string print('Python Programming'.rindex('o')) print('Python Programming Python language'.rindex('Flutter')) // substring not present
Output
7 19 ValueError: substring not found 9 ValueError: substring not found
This is how Python string rindex method works. You can try it with even more examples to better understand the working of this method.
Example 2: rindex() Method with Specified Range
print('Python program'.rindex('Python',0)) print('Python program'.rindex('program',4,14)) print('Python program'.rindex('Python',5)) print('Python program'.rindex('program',1,4))
Output
0 7 ValueError: substring not found ValueError: substring not found
- In example 1, the range starts from 0 index which covers the whole substring.
- In example 2, the starting and ending point is specified and in this range, the substring is present.
- In example 3, we’ve specified a starting range that doesn’t include all the characters of the specified substring. As a result, this method raises an exception.
- In example 4, we’ve specified a starting point and ending point. Ending point ends before the specified substring so as a result, the method raises an exception.
Do try it with other examples to properly understand the usage of starting and ending point in Python string rindex method.
Example 3: Python String rindex() Method Return Value
val= 'Python Programming Language Python' returnedVal=val.rindex('Pytho') print(returnedVal)
Output
28 // highest index value of specified substring is 28
We can see that Python string rindex method has returned the highest index value of specified substring.
The only difference between Python string rindex() and Python string rfind() method is that rindex method raises an exception when substring is not found while rfind method return -1.
Conclusion
To conclude this tutorial, hope you now have a detailed practical understanding of how to properly use Python string rindex method. I’d be very happy to receive your feedback on this post. Thank you for reading it.