In this tutorial, you’ll learn how to use the Python rfind()
method, which uses Python to find the last index substring in a string. Essentially, this method searches from the right and returns the index of the right-most substring.
You’ll learn how to use the Python .rfind()
method, how to use its parameters, how it’s different from .rindex()
, and how to use the method to see if a substring only exists once in a string.
The Quick Answer: Return the Index of the Right-Most Occurrence of a Substring
Table of Contents
Python rfind: Find the Index of Last Substring in String
The Python .rfind()
method works similar to the Python .find()
method.
The .find()
method searches for a substring in a string and returns its index. However, the .rfind()
method searches from the right side of the text and returns the first index it finds – meaning, it returns the last index of a given substring in a string.
Some important things to note about the .rfind()
method:
- The index returned marks the starting index of a substring (i.e., a multi-letter substring would return the index of the first letter)
- If we substring doesn’t exist, then the method returns
-1
Let’s see how we can use the .rfind()
method to find the index of the last substring of a string in Python:
# Use Python .rfind() to Find Index of Last Substring
sentence = 'She sells seashells by the sea-shore'
idx = sentence.rfind('sea')
print(idx)
# Returns 27
We can see here that the method returned the index position of 27
.
Now let’s search for a substring that we know doesn’t exist:
# Use Python .rfind() to Find Index of Last Substring
sentence = 'She sells seashells by the sea-shore'
idx = sentence.rfind('buys')
print(idx)
# Returns -1
We can see here that searching for ‘buys’ returns an index position of -1
, indicating that the substring doesn’t exist in the string.
In the next section, you’ll learn how to use the Python .rfind()
and its parameters to search within a substring of a larger string.
Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!
How to Use Python rfind In a Substring
The Python .rfind()
method also lets you search within a substring of a larger string, by using its start=
and end=
parameters.
Let’s see how we can limit the substring search:
# Use Python .rfind() to Find Index of Last Substring Using start= and end=
sentence = 'She sells seashells by the sea-shore'
print(sentence.rfind('sea', 5, 20)) # Searches: 'ells seashells '
print(sentence.rfind('sea', 0, 30)) # Searches: 'She sells seashells by the sea'
print(sentence.rfind('sea', 0, -1)) # Searches: 'She sells seashells by the sea-shor'
# Returns:
# 10
# 27
# 27
We can see here that the Python .rfind()
method can be used to only search in substrings of a larger string.
In the next section, you’ll learn the differences between two very similar methods, the rfind and rindex methods.
Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.
Differences Between Python rfind and rindex
Python has two very similar methods: .rfind()
and .rindex()
.
The two methods do exactly the same thing, with one notable difference: the .rfind()
method will not fail is a given substring doesn’t exist, while the .rindex()
method will raise a ValueError
.
Let’s see how this works in practice:
# Difference between .rfind() and .rindex()
sentence = 'She sells seashells by the sea-shore'
print(sentence.rfind('buys'))
print(sentence.rindex('buys'))
# Returns:
# -1
# ValueError: substring not found
We can see here that when searching for a substring that doesn’t exist, the .rfind()
method will allow your program to continue.
In the next section, you’ll learn how to use the rfind method to see if a substring only exists once in a Python string.
Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings!
Check if a substring only exists one time in a Python String
There may be times when you want to see if a substring only exists once in a Python string.
In order to do this, you can use the Python .rfind()
method in conjunction with the Python.find()
method.
The way that this will work is to compare whether the two methods return the same index. The reason behind this is that if the index of the first and the last instance of a substring is the same, then the substring only exists a single time.
# Check if a substring only exists once in a string
sentence = 'She sells seashells by the sea-shore'
def single_substring(string, substring):
return string.rfind(substring) == string.find(substring)
print(single_substring(sentence, 'seashells'))
print(single_substring(sentence, 'sea'))
# Returns:
# True
# False
Let’s explore what we’ve done here:
- We defined a function
single_substring()
, which takes a string and a substring as its arguments - The function returns a boolean value by evaluating whether the output of the
.rfind()
and the.find()
methods are the same - The function will return
True
if the string only exists once andFalse
if it exists multiple times (or not at all)
In this section, you learned how to use the Python rfind method to see if a string only exists a single time in a larger string.
Want to learn how to use the Python zip()
function to iterate over two lists? This tutorial teaches you exactly what the zip()
function does and shows you some creative ways to use the function.
Conclusion
In this tutorial, you learned how to use the Python .rfind()
method. The method searches a Python string for a substring and returns the index of the rightmost substring. If no substring is found, then the value of -1
is returned.
You learned how to use the method and all of its arguments. You also learned how to use the method’s arguments to search only in a substring. You also learned the differences between the Python .rfind()
and .rindex()
methods. Finally, you learned how to implement the .rfind()
method from scratch.
To learn more about the .rfind()
method, check out the official documentation here.