In this tutorial, you will learn about the Python string rfind() method along with examples. rfind function in Python returns the highest index of the matching string.
In the previous tutorial, we have seen the Python string rsplit() method to split the string from the right of the string.
Headings of Contents
Python string rfind() method
Python rfind() string method finds a substring in the string and returns the highest index of that string.
Python string rfind() returns -1 if substring not found.
Python rfind method is almost the same as Python string rindex() method.
Syntax
The syntax of the rfind function in Python is:-
string.rfind(sub, start, end)
Parameter
The rfind() function in python accept three parameters: sub, strat and end.
- sub:- substring to be searched.
- start:- starting index to start searching.
- end:- end index to stop searching.
Return Value
The Python python string rfind() return either highest index of substring or -1.
Python string rfind() Example
Here we will write some Python code to understand rfind() method.
Example 1:
a = 'Python is Programming Language'
result = a.rfind('e')
print(result)
Output will be:- 29
Example 2:
a = 'Programming Funda'
result = a.rfind('n')
print(result)
Output will be:- 14
Example 3:
a = 'Programming Funda'
result = a.rfind('is')
print(result)
In the above example output will be -1 because the specified substring not included in string a.
Conclusion:
In this tutorial, you have learned the Python string rfind method to search substring from the string and return the highest index of the substring. if the substring not found, it will return -1.
Python string rfind() almost the same as string rindex() method. I hope this tutorial will help you. if you like this article, please share it with your friends who want to learn Python programming.
Other string methods
- String endswith() method
- String casefold() method
- String center() method
- String capitalize() method
- String count() method
- String index() method
- String format() method
- String isalpha() method
- String isidentifier() method
- String islower() method
- String isupper() method
For More Information:- Click Here