Skip to content
Related Articles

Related Articles

Improve Article
Python – Replace K with Multiple values
  • Last Updated : 03 Jul, 2020

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 replacment.




# 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 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :