Python String rfind() Method
Python String rfind() method returns the rightmost index of the substring if found in the given string. If not found then it returns -1.
Python String rfind() Method Syntax
Syntax: 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.
Return: Returns the right-most index of the substring if it is found in the given string; if not found, then it returns -1.
Python String rfind() Method Example
Example 1: Basic usages of Python String find() Method
Substring 'geeks' found at index : 10 Substring 'for' found at index : 6 Substring 'ate' found at index : 13
Example 2: Using Python String rfind() Method with given start and end position inside String
10 10 -1 6 10
Example 3: Practical Application
Here, we check one email address and the Top Level Domain (TLD) matching our necessary condition. Then we print, “Email matched” else “Email not matched”, followed by the TLD String. Even if this email address contains a “.com” substring, rfind() helped to extract the TLD string more efficiently.
Email not matched, tld: .xyz