Skip to main content

Python String rfind() Function

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.

note

The rfind() method should be used only if you need to know the position of string to be searched.

To check if sub is a substring or not, use the in operator.

note

If you want to get the first occurrence of the specified string, then you have to use the find() method.

Syntax

my_string.rfind(sub, start, end)

rfind() Parameters

Python String rfind() function parameters:

ParameterConditionDescription
subRequiredAny string you want to search for
startOptionalAn index specifying where to start the search. Default value is 0.
endOptionalAn index specifying where to stop the search. Default is the end of the string.

rfind() Return Value

Python String rfind() function 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: 44

output

44

If 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: -1

output

-1

Example 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"

# find()
result = my_str.rfind(sub)
print(result) # Output: 44

# find() after 4th index
result = my_str.rfind(sub, 4)
print(result) # Output: 44

# find() between 4th and 6th index
result = my_str.rfind(sub, 4, 6)
print(result) # Output: -1

output

44
44
-1

Example 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 found

output

-1
Traceback (most recent call last):
File "main.py", line 6, in <module>
result = my_str.rindex('Manager')
ValueError: substring not found