Python – Replace K with Multiple values

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.