Python String rfind()

The Python String rfind() method is a built-in function that returns the substring’s highest index (last occurrence) in a given string. If not found, it returns -1.

Syntax

The Syntax of rfind() method is:

str.rfind(sub, start, end)

Parameters

The rfind() method can take three parameters.

  • sub – substring that needs to be searched in the given string.
  • start (optional) – starting position where the substring needs to be searched within the string
  • end (optional) – ending position where the substring needs to be searched within the string
Note: If the start and end indexes are not provided, by default, it takes 0 as the starting index and str.length-1 as the end index

Return Value

The rfind() method returns an integer value, an index of a substring.

  • If the substring is found in the given string, the rfind() method will return the highest index of the substring.
  • If the substring is not found in the given string, it returns -1
Python Find() Vs Rfind()
Python String rfind() 2

Difference between rfind() method and rindex() method

The rfind() method is similar to rindex() method. The only major difference is that the rfind() method returns -1 if the substring is not found in a given string, whereas the rindex() method will raise the ValueError: substring not found exception.

Example 1: rfind() method without any arguments

text = 'she sells seashells on the seashore'

# find the index of last occurence of substring 'sea'
output = text.rfind('sea')
print("The index of last occurrence of 'sea' is:", output)

# find the index of character 'lls'
output = text.rfind('lls')
print("The index of last occurrence of 'lls' is:", output)

# find the index of substring 'fish', returns -1
output = text.rfind('fish')
print("The index of last occurrence of 'fish' is:", output)

Output

The index of last occurrence of 'sea' is: 27
The index of last occurrence of 'lls' is: 16 
The index of last occurrence of 'fish' is: -1

Example 2: rfind() method with start and end Arguments

text = 'she sells seashells on the seashore'

# find the index of last occurence of substring 'sea'
output = text.rfind('sea', 25)
print("The index of last occurrence of 'sea' is:", output)

# find the index of character 'lls'
output = text.rfind('lls', 1, 10)
print("The index of last occurrence of 'lls' is:", output)

# find the index of substring 'fish', returns -1
output = text.rfind('fish', 1, 50)
print("The index of last occurrence of 'fish' is:", output)

# find the index of substring 's'
output = text.rfind('s', 1, -20)
print("The index of last occurrence of 's' is:", output)

Output

The index of last occurrence of 'sea' is: 27
The index of last occurrence of 'lls' is: 6
The index of last occurrence of 'fish' is: -1
The index of last occurrence of 's' is: 13
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Numpy.repeat() Function

numpy.repeat() Function

Table of Contents Hide SyntaxRepeat ParametersReturn ValueExample 1: Repeating the Single-Dimensional NumPy ArrayExample 2: Repeating the Two-Dimensional NumPy ArrayExample 3: Repeating the NumPy Array Row WiseExample 4: Repeating the NumPy…
View Post