DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

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.

Also read How to Fix TypeError: ‘numpy.float64’ object cannot be interpreted as an integer

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)