In this section, we will learn what String rfind() method is and how to use it in Python.
What is String rfind() Method in Python?
The Python string rfind() method is used to find the index number of the last occurrence of a value in a string.
Python String rfind() Method Syntax:
string.rfind(value, start, end)
String rfind() Method Parameter
The method takes 3 arguments:
- The first argument is the value that we want to look for in the target string.
- The second argument is the index number where we want this search to start in the string. This value is optional and the default is set to 0.
- The last argument is the index number where we want this search to end there. This value is optional and the default is set to the size of the string.
String rfind() Method Return Value
The return value of this method is the index number of the target value’s last occurrence (the first argument of the method).
Note: if the target string didn’t have the specified value (the first argument) then the return value of this method becomes -1.
Example: using python string rfind() method
s1 = "*************My name is John and my last name is Doe**************" print(s1.rfind("is")) print(s1.find("is"))
Output:
46 21