Replace Last Occurrence of Expression in a String in Python



In this article, we are going to learn how to replace the last occurrence of an expression in a string.

In Python, String manipulation is a common task, and Python provides the built-in method named replace(). Though we can replace the specified character or substring in a string using this method, it does not directly support replacing the last occurrence. To achieve this we need to use slicing or a regular expression.

Using Python rfind() Method

The first approach is by using the Python rfind() method searches for the starting index of the last occurrence of the specified substring.

Here, we are going to use the rfind() to return the highest index where the substring is found and use slicing to construct a new string, replacing the returned substring.

Syntax

Following is the syntax of the Python rfind() method -

str.rfind(value, start, end)

Example

Let's look at the following example, where we are going to replace the last occurrence of 'BMW' with 'Vespa' in the string "Rx100, BMW, Activa, BMW".

str1 = "Rx100, BMW, Activa, BMW"
x = "BMW"
y = "Vespa"
demo = str1.rfind(x)
if demo != -1:
    result = str1[:demo] + y + str1[demo + len(x):]
else:
    result = str1
print(result)

The output of the above program is as follows -

Rx100, BMW, Activa, Vespa

Using Python rsplit() Method

The Python rsplit() method is used to split the string into a list of substrings, starting from the right end of the string.

In this approach, we are using the rsplit() method to split the string at the last occurrence of the provided substring and using the join() method to replace the last occurrence.

Syntax

Following is the syntax of the Python rsplit() method -

str.rsplit(separator, maxsplit)

Example

Consider the following example, where we are going to use the startswith() method to check whether the string starts with a capital letter.

str1 = "Hi, Hello, Hi, Namaste"
x = str1.rsplit("Hi", 1)
result = "Vanakam".join(x)
print(result)

The output of the above program is as follows -

Hi, Hello, Vanakam, Namaste

Using Python re.sub() Method

The third approach is by using the Python re.sub() method, which is used to substitute the occurrence of a pattern in a string with another string.

Here, we are going to reverse the string and pattern to use the re.sub() and replace the first match (originally the last). After retrieving the result, we will reverse it to restore the original order.

Example

In the following example, we are going to replace the last occurrence of the '112' with 'ABC' in the string '112-232-112-345'.

import re
str1 = "112-232-112-345"
pattern = "112"
x = "ABC"
a = str1[::-1]
b = pattern[::-1]
c = x[::-1]
replaced = re.sub(b, c, a, count=1)
result = replaced[::-1]
print(result)

The output of the above program is as follows -

112-232-ABC-345
Updated on: 2025-05-20T13:33:55+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements