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

Get notified of the best deals on our WordPress themes.

You May Also Like
Python List Index()

Python List index()

Table of Contents Hide Syntax of List index() index() ParametersReturn Value from List index()Example 1: Find the index of the elementExample 2: Index of the Element not Present in the List…
View Post
Python String Split()

Python String split()

Table of Contents Hide split() Syntaxsplit() Parameterssplit() Return ValueExample 1: How split() works in Python?Example 2: How split() works when maxsplit is specified? The Python String split() method is a…
View Post
Python Any()

Python any()

Table of Contents Hide any() Syntaxany() Parametersany() Return ValueExample 1 – Using any() function on Python ListsExample 2 – Using any() function on Python StringsExample 3 – Using any() function…
View Post
Python Remove Newline From String

Python Remove Newline From String

Table of Contents Hide Python Remove Newline From StringUsing strip() method to remove the newline character from a stringUsing replace() method to remove newlines from a stringUsing regex to remove…
View Post