• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / rfind Python – Find Last Occurrence of Substring in String

rfind Python – Find Last Occurrence of Substring in String

February 21, 2022 Leave a Comment

In Python, the string rfind() function is very useful. We can use the rfind() Python function to find the last occurrence in a string of a character or substring.

string_variable = "This is a string variable we will search."

pos_of_last_a = string_variable.rfind("a")
pos_of_last_w = string_variable.rfind("w")

print(pos_of_last_a)
print(pos_of_last_w)

#Output:
36
29

When working with string variables in Python, being able to search and get the position of substrings and characters in string variables is very useful.

One such capability is to find the first or last occurrence of a substring in a string.

There are many great string methods in Python, and we can find the last occurrence of a character or substring using the Python string rfind() function.

The rfind() Python function, or reverse find function, returns the position of the last occurrence of a given substring in a string.

If rfind() doesn’t find the given substring, then it returns -1.

Below is an example in Python to find the position of the last time a string appears in another string using rfind().

string_variable = "This is a string variable we will search to try to find some other strings."

pos_of_last_a = string_variable.rfind("a")
pos_of_last_to = string_variable.rfind("to")
pos_of_last_string = string_variable.rfind("string")

print(pos_of_last_a)
print(pos_of_last_to)
print(pos_of_last_string)
print(pos_of_last_z)

#Output:
36
48
67
-1

If you want to do the opposite and find the first occurrence in a string using Python, you can use the find() function.

Using the Start and End Arguments of the Python rfind() Function

The Python rfind() function allows us to pass optional “start” and “end” arguments to only search a specific substring of the string for the occurrence of a character or substring.

For example, if we only wanted to search the first 10 characters, we would pass “0” and “10” to rfind().

string_variable = "This is a string variable we will search to try to find some other strings."

pos_of_last_a = string_variable.rfind("a")
pos_of_last_a_first_10 = string_variable.rfind("a",0,10)

print(pos_of_last_a)
print(pos_of_last_a_first_10)

#Output:
36 
8

Hopefully this article has been useful for you to learn how to use the Python rfind() function to find the last occurrence in string of a character or substring using Python.

Other Articles You'll Also Like:

  • 1.  Convert List to Set with Python
  • 2.  Python Check if Object is Iterable with hasattr() Function
  • 3.  How to Shutdown Computer with Python
  • 4.  Using Python to Split a Number into Digits
  • 5.  Python turtle Colors – How to Color and Fill Shapes with turtle Module
  • 6.  Intersection of Two Lists in Python
  • 7.  Get Substring from String in Python with String Slicing
  • 8.  Get Days in Month from Date in pandas DataFrame
  • 9.  pandas Drop Columns – Delete Columns from a DataFrame
  • 10.  Python Split List into N Sublists

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy