Python String rfind() Method: Complete Guide

Python rfind() method finds a substring in the string and returns the highest index. It means it returns the index of the most rightmost matched substring of the string. It returns -1 if a substring is not found.

Python String rfind()

Python string rfind() is a built-in handling function that returns the highest index of the substring that is passed in the rfind() function as an argument. If that substring is not present in the main string, the method returns -1.

There are three arguments passed, first is the substring, starting and ending indexes of the main string we want to search. To give starting and ending indexes is optional, and if we don’t give it, 0 and length of string -1 are taken as default.

Syntax

str.rfind(substring, start_index, end_index)

Here str is the primary string variable. Substring is a part of the main string whose index we will find using this method. Start and end indexes are the index, which is the range in which we’ll find the substring.

Parameters

It takes 3 parameters first is the substring, the second is the starting index, and the last is the ending index.

Return Value

It returns the highest index of a substring in the main string; if that substring is not found, it returns -1.

Example programs on rfind() method in Python

Write a program to show the working of the rfind() method without using the starting and ending indexes.

# app.py

h1= "Hello I love AppDividend"
h2= "AppDividend is the best site for learning"
h3="Hello My name is David Letterman"
h4="I love Python"
h5="Python language doesn't bite"

print("String: ",h1,"Highest Index: ",h1.rfind("love"))
print("String: ",h2,"Highest Index: ",h2.rfind("for"))
print("String: ",h3,"Highest Index: ",h3.rfind("David"))
print("String: ",h4,"Highest Index: ",h4.rfind("Python"))
print("String: ",h5,"Highest Index: ",h5.rfind("drink"))

Output

String:  Hello I love AppDividend Highest Index:  8
String:  AppDividend is the best site for learning Highest Index:  29
String:  Hello My name is David Letterman Highest Index:  17
String:  I love Python Highest Index:  7
String:  Python language doesn't bite Highest Index:  -1

Example 2: Write a program to show the working of the rfind() method using the starting and ending indexes.

See the following code.

# app.py

h1 = "Hello I love AppDividend"
h2 = "AppDividend is the best site for learning"
h3 = "Hello My name is David Letterman"
h4 = "I love Python"
h5 = "Python language doesn't bite"

print("String: ", h1, "Highest Index: ", h1.rfind("love", 3, 14))
print("String: ", h2, "Highest Index: ", h2.rfind("for", 6, 32))
print("String: ", h3, "Highest Index: ", h3.rfind("David", 3, 25))
print("String: ", h4, "Highest Index: ", h4.rfind("Python", 2, 14))
print("String: ", h5, "Highest Index: ", h5.rfind("drink", 3, 16))

Output

String:  Hello I love AppDividend Highest Index:  8
String:  AppDividend is the best site for learning Highest Index:  29
String:  Hello My name is David Letterman Highest Index:  17
String:  I love Python Highest Index:  7
String:  Python language doesn't bite Highest Index:  -1

That’s it for this tutorial.

See also

Python string partition()

Python string swapcase()

Python string maketrans()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.