Python - Replace rear word in String

In this article, we will learn how to replace the rear (last) word in a string with any other given word. This is a common string manipulation task in Python programming.

Let's understand this with an example ?

original_string = "This cat is running very Fast"
# We want to replace "Fast" with "Slow"
# Result: "This cat is running very Slow"

Method 1: Using split() and join()

The most straightforward approach is to split the string into words, replace the last word, and join them back ?

def replace_rear(text, new_word):
    words = text.split()
    words[-1] = new_word
    return ' '.join(words)

original_string = "This cat is running very Fast"
print("Original string:", original_string)
result = replace_rear(original_string, "Slow")
print("After replacement:", result)
Original string: This cat is running very Fast
After replacement: This cat is running very Slow

Method 2: Using Regular Expressions

Regular expressions provide a powerful pattern-matching approach to replace the last word ?

import re

def replace_rear(text, new_word):
    return re.sub(r'\b\w+\b$', new_word, text)

original_string = "This cat is running very Fast"
print("Original string:", original_string)
result = replace_rear(original_string, "Slow")
print("After replacement:", result)
Original string: This cat is running very Fast
After replacement: This cat is running very Slow

Method 3: Using rsplit()

The rsplit() method splits from the right side, making it efficient for replacing the last word ?

def replace_rear(text, new_word):
    parts = text.rsplit(None, 1)  # Split only once from right
    return parts[0] + ' ' + new_word

original_string = "This cat is running very Fast"
print("Original string:", original_string)
result = replace_rear(original_string, "Slow")
print("After replacement:", result)
Original string: This cat is running very Fast
After replacement: This cat is running very Slow

Method 4: Using String Slicing

String slicing with rindex() finds the last space and replaces everything after it ?

def replace_rear(text, new_word):
    last_space_index = text.rindex(' ')
    return text[:last_space_index + 1] + new_word

original_string = "This cat is running very Fast"
print("Original string:", original_string)
result = replace_rear(original_string, "Slow")
print("After replacement:", result)
Original string: This cat is running very Fast
After replacement: This cat is running very Slow

Comparison

Method Complexity Best For
split() + join() Simple General use, readable
Regular Expression Complex Pattern matching needs
rsplit() Moderate Efficient for last word
String Slicing Moderate Memory efficient

Conclusion

The split() and join() method is the most readable and commonly used approach. Use rsplit() for better efficiency when dealing with long strings, and regular expressions when you need advanced pattern matching.

Updated on: 2026-03-27T14:40:08+05:30

252 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements