The String rfind() method searches for the last occurrence of the given string and returns its index. If specified string is not found, it returns -1.
The optional arguments start and end can be used to limit the search to a particular substring of the string.
Syntax
my_string.rfind(sub, start, end)rfind() Parameters
Python String rfind() method parameters:
| Parameter | Condition | Description |
|---|---|---|
sub |
Required | Any string you want to search for |
start |
Optional | An index specifying where to start the search. Default value is 0. |
end |
Optional | An index specifying where to stop the search. Default is the end of the string. |
rfind() Return Value
Python String rfind() method returns an integer value:
- If the substring exists inside the string, it returns the index of the last occurence of the substring.
- If a substring does not exist inside the string, it returns
-1.
Examples
Example 1: Find if a given String is contained in a String
If the given string is contained in the string, then rfind() method returns the index of the last occurence:
my_str = 'Tom is a Developer on tutorialreference.com Development Team'
result = my_str.rfind('Dev')
print(result) # Output: 44output
44If the given string is not found in the string, then rfind() method returns -1.
my_str = 'Tom is a Developer on tutorialreference.com Development Team'
result = my_str.rfind('Ryan')
print(result) # Output: -1output
-1Example 2: Find if a given String is contained in a String with start and end indexes
If you want to limit the search to a substring of the given string, you can specify the start parameter and/or the end parameter.
my_str = 'Tom is a Developer on tutorialreference.com Development Team'
sub = "Dev"
result = my_str.rfind(sub)
print(result) # Output: 44
result = my_str.rfind(sub, 4)
print(result) # Output: 44
result = my_str.rfind(sub, 4, 6)
print(result) # Output: -1output
44
44
-1Example 3: Check if a given String exists in a String
The rfind() method can be used to check if a given string is contained in a string: just check the returned integer.
my_str = "Python is powerful."
sub = "Python"
if my_str.rfind(sub) != -1:
print(f"The substring '{sub}' exists in the text.")
else:
print("The substring does not exist in the text.")output
The substring 'Python' exists in the text.However, it is not meant to be used in this way! To check if sub is a substring or not, use the in operator.
rfind() method vs rindex() method
The rfind() method is identical to the rindex() method.
The only difference is that the rindex() method raises a ValueError exception, if the substring is not found.
An example to compare rfind() and rindex():
my_str = 'Tom is a Developer at ABC'
result = my_str.rfind('Manager')
print(result) # Output: -1
my_str = 'Tom is a Developer at ABC'
result = my_str.rindex('Manager') # Triggers ValueError: substring not foundoutput
-1
Traceback (most recent call last):
File "main.py", line 6, in <module>
result = my_str.rindex('Manager')
ValueError: substring not found