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.

In this article, we will learn about the Python String rfind() method with the help of examples.

rfind() Syntax

The Syntax of rfind() method is:

str.rfind(sub, start, end)

rfind() 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

rfind() 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() 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() 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.

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
Python Bool()

Python bool()

Table of Contents Hide bool() Syntax bool() Parametersbool() Return ValueExample of bool() function in Python Python’s bool() function converts a given value into Boolean(True or False) using the standard truth testing…
View Post
Python String Strip()

Python String strip()

Table of Contents Hide strip() Syntaxstrip() Parametersstrip() Return ValueExample: Working of the strip() methodExample 2 – How to use strip() method in real world? The Python String strip() method is…
View Post
Python String Isdigit()

Python String isdigit()

Table of Contents Hide What are the valid digits in Python?isdigit() Syntaxisdigit() Parameters isdigit() Return valueExample 1: Working with Python string isdigit() methodExample 2: How do I check if a string…
View Post