How does rfind() work in Python?
Overview
The rfind() method in Python returns the highest index of a substring in a string.
Syntax
str.rfind(substring, start, end)
Parameters
One substring must be passed as a parameter, which is searched for in the string upon calling the method.
The search can be limited to indices specified by the start and end parameters. The default value for start is 0, while the default value for end is the end of the string.
Return value
If the substring is found, the method returns the highest index of that substring’s occurrence in the string.
However, -1 is returned if the substring does not exist in the given string.
Code
Example #1
string = "This is a string"x = string.rfind('a')print(x)
Example #2
string = "This is a string"x = string.rfind('i', 1, 4)print(x)
Example #3
string = "This is a string"x = string.rfind('z')print(x)