Python – Replace Different characters in String at Once

Given a mapping of characters to be replaced with corresponding values, perform all replaces at one, in one liner.

Input : test_str = ‘geeksforgeeks is best’, map_dict = {‘e’:’1′, ‘b’:’6′}
Output : g11ksforg11ks is 61st
Explanation : All e are replaced by 1 and b by 6.

Input : test_str = ‘geeksforgeeks’, map_dict = {‘e’:’1′, ‘b’:’6′}
Output : g11ksforg11ks
Explanation : All e are replaced by 1 and b by 6.

Method #1 : Using join() + generator expression 

In this, we perform the task of getting characters present in dictionary and map them to their values by dictionary access, all other characters are appended unchanged, the result is converted back to dictionary using join().



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Replace Different characters in String at Once
# using join() + generator expression 
  
# initializing string
test_str = 'geeksforgeeks is best'
  
# printing original String
print("The original string is : " + str(test_str))
  
# initializing mapping dictionary
map_dict = {'e':'1', 'b':'6', 'i':'4'}
  
# generator expression to construct vals 
# join to get string 
res = ''.join(idx if idx not in map_dict else map_dict[idx] for idx in test_str)
  
# printing result 
print("The converted string : " + str(res)) 

chevron_right


Output

The original string is : geeksforgeeks is best
The converted string : g11ksforg11ks 4s 61st

Method #2 : Using regex + lambda

This is complex way to approach problem. In this, we construct appropriate regex using lambda functions and peform the required task of replacement.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Replace Different characters in String at Once
# using regex + lambda
import re
  
# initializing string
test_str = 'geeksforgeeks is best'
  
# printing original String
print("The original string is : " + str(test_str))
  
# initializing mapping dictionary
map_dict = {'e':'1', 'b':'6', 'i':'4'}
  
# using lambda and regex functions to achieve task 
res = re.compile("|".join(map_dict.keys())).sub(lambda ele: map_dict[re.escape(ele.group(0))], test_str)
  
# printing result 
print("The converted string : " + str(res)) 

chevron_right


Output

The original string is : geeksforgeeks is best
The converted string : g11ksforg11ks 4s 61st

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.