Skip to content
Related Articles

Related Articles

Python – Replace Different characters in String at Once

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 18 Nov, 2022
Improve Article
Save Article

Given a mapping of characters to be replaced with corresponding values, perform all replacements 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 the dictionary and map them to their values by dictionary access, all other characters are appended unchanged, and the result is converted back to the dictionary using join() method. 

Python3




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

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 perform the required task of replacement.

Python3




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

Output

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

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #3 : Using keys() and replace() methods

Python3




# Python3 code to demonstrate working of
# Replace Different characters in String at Once
 
# 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'}
for i in test_str:
    if i in map_dict.keys():
        test_str=test_str.replace(i,map_dict[i])
 
# printing result
print("The converted string : " + str(test_str))

Output

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!