Python - Replace multiple characters at once


In this article we will see method to replace the multiple characters at once in any string. While working with programs where we face such situation when we will have to replace any particular character simultaneously at all of its occurences. Suppose a situation in which you have an article in which it contains one word named as “word” which is having multiple occurrences and you want to convert that “word” into “work” so you can do this manually but it is very bad practice of doing this work one by one. So, we will look at multiple methods using which we can solve this problem.

Method 1. Using the Str.replace() Method.

This is a basic method in which we use the replace function to replace the desired characters.

Example

string = "Toal Tap if Tol on Treep is obvus"
replace_dict= {"T": "S", "o": "w"}
for old, new in replace_dict.items():
   string = string.replace(old, new)
print(string)

Output

Swal Sap if Swl wn Sreep is wbvus

Explanation

From the output you can observe where the character T was there is replaced with S and character o is replaced with w. We used a dictionary which represents key as the character which we want to replace and value as the character which we want to replace the key with. We will traverse and find the value which has to be replaced in the dictionary and replace using the replace() function.

Method 2. Using Regular Expressions.

This is good feature of python and it allows us to find the pattern present in the string. Using this method, we can easily find patterns in complex strings also.

Example

import re
string_text = "Toal Tap if Tol on Treep is obvus"
replace_dict = {"T": "S", "o": "w"}
pattern = re.compile("|".join(map(re.escape, replace_dict.keys())))
string_text = pattern.sub(lambda match: replace_dict[match.group(0)], string_text)
print(string_text)

Output

Swal Sap if Swl wn Sreep is wbvus

Explanation

Here in the above program we used re.sub() function from regular expressions for performing the replacement. Here We created a pattern by joining keys from dictionary. We use the re.sub() method which replaces the matches pattern in the string.

Method 3. Using List Comprehension and Str.join().

In this method we combined the List Comprehension with str.join() method to replace the character.

Example

import re
string_text = "Toal Tap if Tol on Treep is obvus"
replace_dict = {"T": "S", "o": "w"}
string_text = "".join([replace_dict.get(c, c) for c in string_text])
print(string_text)

Output

Swal Sap if Swl wn Sreep is wbvus

Explanation

Here in the above program we use the list comprehension for traversing on each character in the string_text. We use get() method of the replacements dictionary for replacing the character if it is present in the dictionary. We use str.join() funcion to join back the modified characters.

Method 4. Using a List Comprehension and Str.replace().

Example

import re
string_text = "Toal Tap if Tol on Treep is obvus"
replace_dict = {"T": "S", "o": "w"}
string_text = "".join([replace_dict.get(c, c).replace(c, replace_dict.get(c, c)) for c in string_text])
print(string_text)

Output

Swal Sap if Swl wn Sreep is wbvus

Explanation

Here in the above program we combined list Comprehension with replace() function which. We traversed every character of the string_text and replaced it with the corresponding value from the replace_dict dictionary. The replace() method will replace only desired characters and other characters will remain the same.

So, we get to know about various methods using which we can replace multiple characters at once within the string. Each of the methods has its own approach to replace multiple occurrences of characters in string at once. You can choose the methods which are suitable for your requirement but having ideas about various methods is good for learning.

Updated on: 03-Oct-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements