Methods and Functions

Python String rfind()

There might arise a situation where we would want to find out the last occurrence of a specific element from a collection of data. To carry out this operation, Python provides us with a string handling function. Python rfind() is used in such cases. In this article, we will learn about the Python rfind() method, its syntax along with the help of various programming examples.

Python rfind() function

Definition

  • Python rfind() function is used to return the highest index value of the last occurrence of the substring from the input string; else it returns -1.
  • The Python rfind() is an in-built string method that returns the index position of the character if found; else it returns value -1.

Python rfind() Syntax

Python rfind() function follows the below-mentioned syntax:

                    

string.rfind(substr, start, end)

Note – The rfind() function is case-sensitive.

rfind() Parameters

Python rfind() string method accepts only 3 parameters:

  • substr – the substring to be searched for in the given string
  • start (Optional) – starting position from where the substr is to be searched. The default value is 0.
  • end (Optional) – ending position till where the substr is to be searched. The default value is the end of the string or length of the string.

Return value from rfind()

The Python rfind() method returns an integer value. There can be 2 situations described as follows:

  • It returns the highest index position of the last occurrence of the substring, only if the substring exists in the input string.
  • If the substring is not found, then the function return value -1.

Example 1: rfind() With No start and end Argument

The rfind() method will look for the substring and return the place of the substring’s last occurrence. If the substring appears numerous times in the given string, it will still return the index or position of the last occurrence (i.e. occurrence of an element from extreme right).

Example

                    

# Python program to illustrate rfind()
txt = 'I will eat, we will eat, they will eat'

# highest occurrence of 'eat'(case sensitive)
result = txt.rfind('eat')
print("Highest Index of Substring 'eat':", result)

# highest occurrence of 'will'
result = txt.rfind('will')
print("Highest Index of Substring 'will':", result)

# character does not exist
result = txt.rfind('ate')
print("Highest Index of Substring 'ate':", result)

Output

                    

Highest Index of Substring 'eat': 35
Highest Index of Substring 'will': 30
Highest Index of Substring 'ate': -1

Example 2: rfind() With start and end Arguments

                    

# Python program to illustrate rfind()
txt = 'I am currently pursuing Masters i.e. MBA in Finance'

# start search from index position 25
print(txt.rfind('ur', 10))

# start and end search from index position 5 to 30
print(txt.rfind('M', 5, 30))

# start and end search from index position 10 to 20
print(txt.rfind('in', 10, 20))

Output

Difference between find() and rfind()

The Python function rfind() is similar to the find() function, with the sole difference being that rfind() returns the highest index (from the right) for the provided substring, whereas find() returns the index position of the element’s first occurrence, i.e. the very first index. If the substring is not found, both rfind() and find() will return -1.

Example

                    

txt = 'Will you text? Or should I text? Is it okay to text?'

print("The position of 'text' using find():", txt.find('text'))
print("The position of 'text' using rfind():", txt.rfind('text'))

print("The position of 'i' using find():", txt.find('i'))
print("The position of 'i' using rfind():", txt.rfind('i'))

Output

                    

The position of 'text' using find(): 9
The position of 'text' using rfind(): 47
The position of 'i' using find(): 1
The position of 'i' using rfind(): 36

Frequently Asked Questions

Q1. What is rfind in Python?

There might arise a situation where we would want to find out the last occurrence of a specific element from a collection of data. To carry out this operation, Python provides us with a string handling function. Python rfind() is used in such cases.

Python rfind() function is used to return the highest index value of the last occurrence of the substring from the input string; else it returns -1.

Python rfind() function follows the below-mentioned syntax:

                    

string.rfind(substr, start, end)

Q2. What is the difference between rfind() and rindex() in Python?

Both the Python rfind() and rindex() methods are the same. They are used to find out the index value of a specified element from the input string. The only difference between them is that the rindex() method throws a ValueError if the substring cannot be found. Whereas the Python rfind() method returns value -1.

Example

                    

txt = 'Cars, Bikes, Planes'

# Using Python rfind()
res = txt.rfind('Cycle')
print(res)

# Using Python rindex()
res = txt.rindex('Cycle')
print(res)

Output

                    

-1
Traceback (most recent call last):
File "", line 8, in 
ValueError: substring not found

Q3. How does rfind method work?

Python rfind() function finds the last occurring specified element in the input string and returns its index position. If the specified string is not found in the input string, then the function returns value -1. Python rfind() function follows the below-mentioned syntax:

                    

string.rfind(substr, start, end)

Python rfind() string method accepts 3 parameters:

  • substr – the substring to be searched for in the given string
  • start (Optional) – starting position from where the substr is to be searched. The default value is 0.
  • end (Optional) – ending position till where the substr is to be searched. The default value is the end of the string or length of the string.

Example

                    

txt = 'Good morning, Good afternoon, Good evening, Good night'

# index position of highest occuring 'Good'
result = txt.rfind('Good')
print("Highest Index of Substring 'Good':", result)

# start and end search from index position 5 to 30
print("Index of 'Good' from Index 5-20:", txt.rfind('Good', 5, 20))

# start and end search from index position 10 to 20
print("Index of 'ing' from Index 5-45:", txt.rfind('ing', 5, 45))

Output

                    

Highest Index of Substring 'Good': 44
Index of 'Good' from Index 5-20: 14
Index of 'ing' from Index 5-45: 39

Q4. What is rindex in Python?

Python rindex() built-in function is used to return the highest index value of the last occurrence of the substring from the input string, if the substring is found. If the substring specified is no found, then it raises a ValueError exception.

The syntax for the Python rindex() string function is as follows:

                    

string.rindex(substr, start, end)

Example

                    

# Python program to illustrate rindex()
s = 'are you having a party at your house?'

# specifying start and end parameters
print("Index of 'a':", s.rindex('a', 5, 30))
print("Index of 'you':", s.rindex('you', 0, 35))

# substring not found
print("Index of 'dinner':", s.rindex('dinner', 0, 30))

Output

                    

Index of 'a': 23
Index of 'you': 26
Traceback (most recent call last):
File "", line 11, in 
ValueError: substring not found

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.