The rfind() method in Python is a string method that is used to find the last occurrence of a specified substring within a string. It returns the highest index of the substring within the string. If the substring is not found, it returns -1.
Parameter Values
| Parameter | Description |
|---|---|
| sub | The |
| start | The |
| end | The |
Return Values
The rfind() method in Python returns an int indicating the last position of a substring.
How to Use rfind() in Python
The rfind() method returns the highest index of the specified substring within the given string. If the substring is not found, it returns -1.
text = 'Python is awesome, Python is great'
print(text.rfind('Python')) # Output: 20If the rfind() method is used with additional parameters, it searches for the substring within the specified range of indices.
text = 'Python is awesome, Python is great'
print(text.rfind('Python', 0, 10)) # Output: -1The rfind() method is case-sensitive, so make sure the substring matches exactly.
text = 'Python is awesome, Python is great'
print(text.rfind('python')) # Output: -1