• 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 / Find Last Occurrence in String of Character or Substring in Python

Find Last Occurrence in String of Character or Substring in Python

February 10, 2022 Leave a Comment

In Python, to find the last occurrence in a string of a character or substring, the easiest way is to use the Python string rfind() function.

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

You can also use the Python string rindex() function, but if the function doesn’t find the character or substring, it will throw an exception.

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

pos_of_last_a = string_variable.rindex("a")
pos_of_last_w = string_variable.rindex("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 the string variables for substrings and characters 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() function, or reverse find function, returns the position of the last occurrence of a given string in a string.

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

Below is some sample Python code to find the last occurrence in string of another string.

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 find the first occurrence in a string using Python, you can use the find() function.

Finding the Last Occurrence in String Using Start and End Arguments With Python rfind() Function

With the Python rfind() function, we can 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

Finding the Last Occurrence in String of Substring Using Python rindex() Function

Another way we can find the last occurrence of a substring or character in a string is with the Python string rindex() function.

The rindex() function is almost the same as the rfind() function, but if the function doesn’t find the given substring or character, rindex() will throw an exception.

Below are a few examples of using the rindex() function in Python to the last occurrence in string of another string.

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

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

print(pos_of_last_a)
print(pos_of_last_w)

#Output:
36 
29

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

Other Articles You'll Also Like:

  • 1.  Using Python to Generate Random String of Specific Length
  • 2.  Python os.sep – Create Operating System Path Seperator Character
  • 3.  Count Letters in Word in Python
  • 4.  How to Declare Variable Without Value in Python
  • 5.  pandas mad – Calculate Mean Absolute Deviation in Python
  • 6.  How to Save and Use Styles from Existing Word File With python-docx
  • 7.  pandas unique – Get Unique Values in Column of DataFrame
  • 8.  Check if Variable is String in Python
  • 9.  Using Python to Read File Word by Word
  • 10.  Check if Number is Larger than N in Python

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