Python String rfind() Method
Python String rfind() method returns the highest index of the substring if found in the given string. If not found then it returns -1.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
str.rfind(sub, start, end)
- sub: It’s the substring that needs to be searched in the given string.
- start: Starting position where the sub needs to be checked within the string.
- end: Ending position where suffix needs to be checked within the string.
Note: If start and end indexes are not provided then, by default it takes 0 and length-1 as starting and ending indexes where ending indexes are not included in our search.
Returns the highest index of the substring if it is found in the given string; if not found, then it returns -1.
ValueError: This error is raised in the case when the argument string is not found in the target string.
Substring 'geeks' found at index : 10 Substring 'for' found at index : 6 Substring 'ate' found at index : 13
10 10 -1 6
Example 3: Practical Application
Useful in string checking. To check if the given substring is present in some string or not.
Doesn't contains given substring