Skip to content
Related Articles

Related Articles

Python – Replace K with Multiple values

Improve Article
Save Article
  • Last Updated : 05 Sep, 2022
Improve Article
Save Article

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




# Python3 code to demonstrate working of
# Replace K with Multiple values
# Using loop + replace()
 
# initializing strings
test_str = '_ is _ . It is recommended for _'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing repl_char
repl_char = '_'
 
# initializing repl_list
repl_list = ['Gfg', 'Best', 'CS']
 
# Replace K with Multiple values
# Using loop + replace()
for ele in repl_list:
    test_str = test_str.replace(repl_char, ele, 1)
 
# printing result
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

  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




# Python3 code to demonstrate working of
# Replace K with Multiple values
# Using split() + join() + zip()
 
# initializing strings
test_str = '_ is _ . It is recommended for _'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing repl_char
repl_char = '_'
 
# initializing repl_list
repl_list = ['Gfg', 'Best', 'CS']
 
# Replace K with Multiple values
# Using split() + join() + zip()
test_list = test_str.split(repl_char)
temp = zip(test_list, repl_list)
res = ''.join([ele for sub in temp for ele in sub])
 
# printing result
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

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Space Complexity: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!