rfind() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

In Python, the rfind() method returns the index of the last occurrence of a substring in the given string, and if not found, then it returns -1.

Syntax of rfind() function in Python

Syntax of the rfind() method in Python is as follows:

The string here is the given string in which the value's last occurrence has to be searched. Three arguments: value, start, and end can be passed to the rfind() method.

Parameters of rfind() in python

Parameters of the rfind() method in Python are as follows:

  • value: The substring to search for in the given string.
  • start (optional): The starting position from where the value needs to be checked in the given string. It is an optional parameter and 0 by default.
  • end (optional): The ending position till where the value needs to be checked in the given string. It is an optional parameter and is at the end of the given string by default.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return Values of rfind() function in Python

The rfind() method returns the index of the last occurrence of a value in the given string. If the value is not found in the given string, then the return value is -1.

Example of rfind() function in Python

Code:

Output:

Explanation:

In the above example, we are finding the index of the last occurrence of val in txt using the rfind() method in Python. The index of the last occurrence of val is found out to be 18.

What is rfind() function in Python?

The rfind() method is an inbuilt string method in Python that returns the index of the last occurrence of a substring in the given string. If the substring is not found in the given string, rfind() returns -1.

rfind function in python

We can use the optional arguments start and end in the rfind() method to specify the start and end of the string in which we have to find the substring. The rfind() is a case sensitive method, "Scaler" and "scaler" are two different words for it.

what is rfind in python

The string rfind() method is very similar to the string rindex() method, both of them return the index of the last occurrence of a substring in the given string, the only difference between them is, when the substring is not found in the given string, the rfind() method will return -1, whereas the rindex() method will raise an error.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

More Examples

Let's take a look at some more examples of the rfind() method in Python for a better understanding:

Example 1: Using rfind() with no start and end argument

Code:

Output:

Explanation:

In the above example, we are finding the index of the last occurrence of "cream" in the string "I scream you scream, we all scream ice-cream" using the rfind() method. As we can see in the given string, the substring "cream" is present at indices 3, 14, 29, and 39. The rfind() method will return the highest index among these indices, that being 39, so the return value will be 39.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example 2: Using rfind() with start and end arguments

Code:

Output

Explanation:

In the above example, we are using the rfind() method to find the index of the last occurrence of "cream" in the string "I scream you scream, we all scream ice-cream" between the starting position - 3 and ending position - 20. As we can see in the given string between the boundaries, the substring "cream" is present at index 14, so the return value will be 14.

Example 3: Behaviour of rfind() and rindex() when the value is not found in the given string

Behaviour of rfind() method

Code:

Output

Explanation:

In the above example, we are finding the index of the last occurrence of "Pizza" in the string "Fried Chicken, Fried rice, Ramen" using the rfind() method. As we can see in the given string, the substring "Pizza" is not present at any position, so the return value will be -1.

Behaviour of rindex() method

Code:

Output:

Explanation:

In the above example, we are finding the index of the last occurrence of "Pizza" in the string "Fried Chicken, Fried rice, Ramen" using the rindex() method. As we can see in the given string, the substring "Pizza" is not present in the given string, so the rindex() method will raise a ValueError, stating that the substring is not found.

Conclusion

  • The string rfind() method returns the index of the last position of a substring in the given string and returns -1 if it is not found.
  • We can specify the starting and ending position of checking the substring in the given string using the start and end parameters.
  • The only difference between the rfind() method and rindex() method is that if the substring is not found in the given string, rfind() method returns -1, whereas rindex() raises an error.
  • The string rfind() method can also be used to check whether a substring is present in the given string or not.

See Also

Refer to these topics below:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more