Python rindex() method returns the last index of the substring inside the string. Otherwise, it raises an exception.
The syntax of the method is: str.rindex(str, start = 0 end = len(string))
Key Points :
• If the passed substring passed by the argument in the main string is not present, this function throws an exception in this case.
• This function passes three arguments: the first argument is substring; the second and third arguments start and end index of the main string in which the substring is to be searched
• As starting an ending index are optional arguments so if in the case these are not passed then start taken as 0 while the length is taken as -1.
• For instance, this method is case sensitive, considering studies and Studies as two different words.
We’ll be covering the following topics in this tutorial:
Python String rindex(): Parameters
The function rindex() accepts three parameters, which are given below:
• str: This is used to define the substring, which is part of the main string, the index of which can be identified with the function rindex().
• start: This is an optional parameter used to denote the default value of the search.
• End: This is also an optional parameter for our search ends in this position.
Python String rindex(): Returned Values
How to find last occurrence of character in string python
This python method finds the last occurrence of character in string. This function’s drawback is that it throws the exception if there is no substring in the string and hence breaks the code.rindex
Below is the python program to demonstrate the rindex() function:
str = 'The Best Learning Resource for Online Education' find = "Online" res = str.rindex(find) print(res)
When we run above the program, the outcome is as follows:
Below are several other functions that we can use to work with strings in Python 3