Table of Contents
The rfind() method returns the highest index of the substring (if found). If not found, it returns -1.
The syntax of rfind() is:
str.rfind(sub[, start[, end]] )
1. rfind() Parameters
rfind() method takes a maximum of three parameters:
- sub – It’s the substring to be searched in the str string.
- start and end (optional) – substring is searched within
str[start:end]
2. Return Value from rfind()
rfind() method returns an integer value.
- If substring exists inside the string, it returns the highest index where substring is found.
- If substring doesn’t exist inside the string, it returns -1.

3. Example 1: rfind() With No start and end Argument
quote = 'Let it be, let it be, let it be'
result = quote.rfind('let it')
print("Substring 'let it':", result)
result = quote.rfind('small')
print("Substring 'small ':", result)
result = quote.rfind('be,')
if (result != -1):
print("Highest index where 'be,' occurs:", result)
else:
print("Doesn't contain substring")
Output
Substring 'let it': 22 Substring 'small ': -1 Highest index where 'be,' occurs: 18
4. Example 2: rfind() With start and end Arguments
quote = 'Do small things with great love'
# Substring is searched in 'hings with great love'
print(quote.rfind('things', 10))
# Substring is searched in ' small things with great love'
print(quote.rfind('t', 2))
# Substring is searched in 'hings with great lov'
print(quote.rfind('o small ', 10, -1))
# Substring is searched in 'll things with'
print(quote.rfind('th', 6, 20))
Output
-1 25 -1 18
Related posts:
Python Namespace and Scope
Python Dictionary copy()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Program to Convert Bytes to a String
Java Program to Permute All Letters of an Input String
Python bool()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Split a String in Java
Python Program to Copy a File
Java InputStream to String
Python float()
Python Program to Compute the Power of a Number
Python tuple()
Python Tuple index()
Most commonly used String methods in Java
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Find the Factorial of a Number
Python Modules
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Merge Two Dictionaries
Python Program to Get the File Name From the File Path
Python Program to Differentiate Between del, remove, and pop on a List
Python for Loop
How to get current date and time in Python?
Python oct()
Python String rpartition()
Python List extend()
String Initialization in Java
Python Program to Count the Number of Each Vowel
Python Program to Safely Create a Nested Directory
Python Decorators
Python Program to Print Hello world!