Replace Last occurrence of a String in Python

This article will discuss different ways to replace only the last occurrence of a substring in a string.

Table Of Contents

Suppose we have a string,

"This is the last rain of Season and Jack is here."

We want to replace the last occurrence of “is” with the “XX”. The final string should be like,

"This is the last rain of Season and Jack XX here."

There are different ways to do this. Let’s discuss them one by one.

Using replace() function.

In Python, the string class provides a function replace(), and it helps to replace all the occurrences of a substring with another substring. We can use that to replace only the last occurrence of a substring in a string.

Steps are,

  • Reverse the string to be replaced.
  • Reverse the replacement string.
  • Reverse the original string and replace the first occurrence of reversed substring * with the reversed replacement string.
  • Then reverse the modified string and assign it back to the original string.

Basically, if we reverse the original string and the substring to be replaced, we need to remove its first occurrence instead of the last. For that, pass the max count as 1 in the replace() function. Then, in the end, we can reverse the modified string again.

For example,

strValue = "This is the last rain of Season and Jack is here."

strToReplace   = 'is'
replacementStr = 'XX'

# Reverse the substring that need to be replaced
strToReplaceReversed   = strToReplace[::-1]
# Reverse the replacement substring
replacementStrReversed = replacementStr[::-1]


# Replace last occurrences of substring 'is' in string with 'XX'
strValue = strValue[::-1].replace(strToReplaceReversed, replacementStrReversed, 1)[::-1]

print(strValue)

Output:

This is the last rain of Season and Jack XX here.

It replaced only the last occurrence of “is” with the “XX” in the string.

Using rfind() function

Search for the index position of the last occurrence of the substring that needs to be replaced from the original string. For that, use the rfind() function of the string class. It returns the highest index of the substring in the string i.e., the index position of the last occurrence of the substring. Then using the subscript operator and index range, replace that last occurrence of substring.

Basically, select all the characters before the last occurrence of substring “is” and then add “XX” to it. Then select all the characters after the last occurrence of the substring “is” and append it to the end of the new string.

For example,

strValue = "This is the last rain of season and Jack is here."

strToReplace   = 'is'
replacementStr = 'XX'

# Search for the last occurrence of substring in string
pos= strValue.rfind(strToReplace)

if pos > -1:
    # Replace last occurrences of substring 'is' in string with 'XX'
    strValue = strValue[:pos] + replacementStr + strValue[pos + len(strToReplace): ]

print(strValue)

Output:

This is the last rain of Season and Jack XX here.

It replaced only the last occurrence of “is” with the “XX” in the string.

Using rsplit() and join()

To replace the last occurrence of a substring from a string, split the string from right using substring as delimiter and keep the maximum count of splits as 1. It will give us a list with two strings i.e.

  • A string containing all the characters before the delimiter substring.
  • A string containing all the characters after the delimiter substring.

Then join these using join() function and use the replacement string as delimiter.

For example,

strValue = "This is the last rain of season and Jack is here."

# Substring that need to be replaced
strToReplace   = 'is'
# Replacement substring
replacementStr = 'XX'

# Replace last occurrences of substring 'is' in string with 'XX'
strValue = replacementStr.join(strValue.rsplit(strToReplace, 1))

print(strValue)

Output:

This is the last rain of Season and Jack XX here.

It replaced only the last occurrence of “is” with the “XX” in the string.

Summary:

We learned about three different ways to replace the last occurrence of a string in Python.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top