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 List remove()
Python Program to Find LCM
Format ZonedDateTime to String
Python Program to Find Factorial of Number Using Recursion
Python String isupper()
Python String rindex()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Check the File Size
Python Program to Check If a String Is a Number (Float)
Python Closures
Python ord()
Check if a String is a Palindrome in Java
Python Variables, Constants and Literals
Convert String to Byte Array and Reverse in Java
Python Program to Print the Fibonacci sequence
Python String format_map()
Python Set difference_update()
Python Set update()
Python String join()
APIs in Node.js vs Python - A Comparison
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python hex()
Python Program to Convert Celsius To Fahrenheit
Python Program to Check if a Number is Positive, Negative or 0
Python Program to Compute the Power of a Number
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Convert String to int or Integer in Java
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Iterate Over Dictionaries Using for Loop
Python sum()
Python Data Types
Python timestamp to datetime and vice-versa