Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Replace rear word in String

Python | Replace rear word in String

Sometimes, while working with String list, we can have a problem in which we need to replace the most rear i.e last word of string. This problem has many application in web development domain. Let’s discuss different ways in which this problem can be solved. 

Method #1 : Using split() + join() This is one way in which we can perform this task. In this, we break elements into parts and then return the last value and perform the addition of new element using join(). 

Python3




# Python3 code to demonstrate working of
# Rear word replace in String
# using split() + join()
 
# initializing string
test_str = "GFG is good"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing replace string
rep_str = "best"
 
# Rear word replace in String
# using split() + join()
res = " ".join(test_str.split(' ')[:-1] + [rep_str])
 
# printing result
print("The String after performing replace : " + res)


Output

The original string is : GFG is good
The String after performing replace : GFG is best

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using rfind() + join() The combination of these functions can also be used to perform this task. In this, we perform the task of extracting the last word of the string using rfind() and join() is used to perform replace. 

Python3




# Python3 code to demonstrate working of
# Rear word replace in String
# using rfind() + join()
 
# initializing string
test_str = "GFG is good"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing replace string
rep_str = "best"
 
# Rear word replace in String
# using rfind() + join()
res = test_str[: test_str.rfind(' ')] + ' ' + rep_str
 
# printing result
print("The String after performing replace : " + res)


Output

The original string is : GFG is good
The String after performing replace : GFG is best

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using split(),pop(),append() and join() methods

Python3




# Python3 code to demonstrate working of
# Rear word replace in String
 
# initializing string
test_str = "GFG is good"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing replace string
rep_str = "best"
 
# Rear word replace in String
x = test_str.split()
x.pop()
x.append(rep_str)
res = " ".join(x)
# printing result
print("The String after performing replace : " + res)


Output

The original string is : GFG is good
The String after performing replace : GFG is best

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4:Using split(),indexing, list(),join() functions

Python3




# Python3 code to demonstrate working of
# Rear word replace in String
 
# initializing string
test_str = "GFG is good"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing replace string
rep_str = "best"
 
list_words = list(test_str.split())
list_words[-1] = rep_str
res = " ".join(list_words)
 
# printing result
print("The String after performing replace : " + res)


Output

The original string is : GFG is good
The String after performing replace : GFG is best

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5 : Using regex

Python3




import re
 
#initializing string
test_str = "GFG is good"
 
#printing original string
print("The original string is : " + test_str)
 
#initializing replace string
rep_str = "best"
 
#Rear word replace in String
res = re.sub(r'\b[\w-]+\b(?=\s*$)', rep_str, test_str)
 
#printing result
print("The String after performing replace : " + res)


Output

The original string is : GFG is good
The String after performing replace : GFG is best

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #6: Using  str.rsplit()

Python3




test_str = "GFG is good"
rep_str = "best"
#printing original string
print("The original string is : " + test_str)
words = test_str.rsplit(maxsplit=1)
words[-1] = rep_str
res = " ".join(words)
print("The String after performing replace : " + res)
#This code is contributed by Jyothi pinjala


Output

The original string is : GFG is good
The String after performing replace : GFG is best

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 7: Using rpartition() function

Step-by-step approach:

  • Initialize the original string and print it.
  • Initialize the replace string.
  • Use rpartition() function to split the string into three parts: string before the last occurrence of space,space, and the last word.
  • Concatenate the first two parts with the replace string and space separator.
  • Store the result in a variable.
  • Print the result.

Python3




# initializing string
test_str = "GFG is good"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing replace string
rep_str = "best"
 
# using rpartition() function to split the string
first, sep, last = test_str.rpartition(' ')
 
# replacing the last word with replace string
last = rep_str
 
# concatenate the first two parts and the replace string with space separator
res = first + sep + last
 
# printing result
print("The String after performing replace : " + res)


Output

The original string is : GFG is good
The String after performing replace : GFG is best

Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(n), for storing the result in a variable.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments