Find Last Occurrence of Substring using Python
Introduction
When planning to work with strings, we may need to determine whether a substring is present. This issue is rather typical, and there have been numerous discussions about how to solve it. Here, it is discussed how to obtain the string's most recent occurrence. Let's talk about a few approaches to use Python to locate the final instance of a substring within a string.
String
A string can be defined as the collecting of characters that can represent a single word or an entire sentence. Python allows us to declare strings and other datatypes without using the datatype constant. Consequently, using strings in Python is simpler than using them in other programming languages.
An entity of said String class, which has numerous methods for manipulating and gaining access to strings, is a string.
Apply rindex() Technique
Using the rindex() method from the built-in Python String class is one technique to determine the index of the final instance of a substring. It takes a string encoding the desired substring as an argument and returns the position of that string's last occurrence in the current string.
This function's primary flaw has been that it raises an exception because the string contains no substrings.
Code
# example python code to find
# the substring’s last occurence
# the last occurrence of substring
# with the help of rindex() method
# declare any example string
string = 'The trees outside my window are green and full of leave'
# declare any word from the string
word = " green "
# with the help of rindex() find
# the last occurrence of the substring
x = string.rindex(word)
# displaying the initialized string
print(' The string is = ' + str(string))
# displaying the index value
print(' Required index of the substring is = ' + str(x))
Output
The string is = The trees outside my window are green and full of leave
Required index of the substring is = 32
Applythe rfind() Technique
There is yet another method called rfind that can be used to get around rindex(). Its behaviour is comparable to that of rindex(), but instead of throwing an error if the requested substring is not found in the string, this technique produces "1," showing that it is just not.
One can specify the starting index from which you would really like to start as well as the end index as alternative options for both rfind() & rindex().
Code
# example python code to find
# the substring’s last occurence
# with the help of rfind() method
# declare any example string
string = 'He is a good student'
# declare any word from the string
word = "is"
# with the help of rfind() find
# the last occurrence of the substring
x = string.rfind(word)
# displaying the initialized string
print(' The string is = ' + str(string))
# displaying the index value
print(' Required index of the substring is = ' + str(x))
Output
The string is = He is a good student
Required index of the substring is = 3
In [5]:
Use Techniques find() and replace()
Code
# example python code to find
# the substring’s last occurence
# with the help of find() and replace()
# declare any example string
string = 'The cat is sleeping on the pillow'
# declare any word from the string
word = "pillow"
# using find() and replace()
var = string.count(word)
i=1
while(i<var):
string=string.replace(word,"*"*len(word),1)
i= i+1
x=string.find(word)
# displaying the initialized string
print(' The string is = ' + str(string))
# displaying the index
print(' Required index of the substring is = ' + str(x))
Output
The string is = The cat is sleeping on the pillow
Required index of the substring is = 27
Combine rlocate() Method with lambda()
Now, we are utilising the rlocate() method of the more itertools package, which enables us to pinpoint the substring's final occurrence within the input string.
Code
# importing the module
import more_itertools as mi
# declare any example string
string = 'My favorite color is blue'
# declare any word from the string
word = "color"
# using lambda
pred = lambda *var: var == tuple(word)
# using rlocate()
x = next(mi.rlocate(string, pred=pred,window_size=len(word)))
# displaying the initialized string
print(' The string is = ' + str(string))
# displaying the index
print(' Required index of the substring is = ' + str(x))
Output
The string is = My favorite color is blue
Required index of the substring is = 12