Sometimes, while working with Python Strings, we can have a problem in which we need to perform replace of single character/work with particular list of values, based on occurrence. This kind of problem can have application in school and day-day programming. Let’s discuss certain ways in which this task can be performed.
Input : test_str = ‘* is * . It is recommended for *’, repl_char = ‘*’, repl_list = [‘Gfg’, ‘Best’, ‘CS’]
Output : Gfg is Best . It is recommended for CS
Input : test_str = ‘* is *’, repl_char = ‘*’, repl_list = [‘Gfg’, ‘Best’]
Output : Gfg is Best
Method #1 : Using loop + replace() The combination of above functions can be used to solve this problem. In this, we perform the task of replacing using replace() and increase the index counter after each replacement.
Python3
test_str = '_ is _ . It is recommended for _'
print ( "The original string is : " + str (test_str))
repl_char = '_'
repl_list = [ 'Gfg' , 'Best' , 'CS' ]
for ele in repl_list:
test_str = test_str.replace(repl_char, ele, 1 )
print ( "String after replacement : " + str (test_str))
|
Output :
The original string is : _ is _ . It is recommended for _
String after replacement : Gfg is Best . It is recommended for CS
Time Complexity: O(n), where n is the length of the original string.
Auxiliary Space: O(1), as the space used is constant and does not depend on the input size.
Method #2 : Using split() + join() + zip() The combination of above functions offer yet another way to solve this problem. In this, we first split the word using split(), zip both required lists using zip() and then rejoin with appropriate replacement.
Python3
test_str = '_ is _ . It is recommended for _'
print ( "The original string is : " + str (test_str))
repl_char = '_'
repl_list = [ 'Gfg' , 'Best' , 'CS' ]
test_list = test_str.split(repl_char)
temp = zip (test_list, repl_list)
res = ''.join([ele for sub in temp for ele in sub])
print ( "String after replacement : " + str (res))
|
Output :
The original string is : _ is _ . It is recommended for _
String after replacement : Gfg is Best . It is recommended for CS
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using regular expressions and a lambda function
Steps:
- Import the re module to use regular expressions.
- Define a lambda function that takes a match object as input and returns the corresponding replacement value from repl_list.
- Use the re.sub() function to replace all occurrences of repl_char in test_str with the values from repl_list using the lambda function.
- Store the result in res.
Python3
import re
test_str = '_ is _ . It is recommended for _'
print ( "The original string is : " + str (test_str))
repl_char = '_'
repl_list = [ 'Gfg' , 'Best' , 'CS' ]
res = re.sub(repl_char, lambda x: repl_list.pop( 0 ), test_str)
print ( "String after replacement : " + str (res))
|
Output
The original string is : _ is _ . It is recommended for _
String after replacement : Gfg is Best . It is recommended for CS
Time complexity: O(n)
Auxiliary space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Apr, 2023
Like Article
Save Article