Python Replace Multiple Characters
In this article, you will see how python replace multiple characters in a string using multiple different methods.
Quick Solution
For you in hurry here are the quick solutions to replace multiple different characters in a string in python.
More methods are discussed below in detail.
# replace multiple characters in a string
# 👉 Method 1
# chaining replace() method
str = "[email protected]"
str = str.replace("@", "_").replace("e", "E").replace("o", "0")
print(str) # HEll0_W0rld
# 👉 Method 2
# replace multiple characters
# when map of characters is given
str = "[email protected]"
repl_map = {"@": "_", "e": "E", "o": "0"}
# loop through the dictionary and replace
for key, value in repl_map.items():
str = str.replace(key, value)
print(str) # HEll0_W0rld
HEll0_W0rld HEll0_W0rld
# 1: chaining replace() method
The str.replace() method is used to replace a part of a string or a character of a string with another string.
It returns a new string with the replaced part.
It takes two compulsory arguments, the first argument is the part of the string to be replaced and the second argument is the string to replace with. Also, it takes an optional argument count which is the number of times to replace the string.
str = "tutorialstonight"
# replace 't' with 'T'
str = str.replace("t", "T")
print(str) # TuTorialsTonighT
Here we have replaces all the t with T. But what if we need to replace more other characters with other characters?
For this, you can either use replace() methods multiple times or you can use the chaining method.
The replace() method supports chaining means you can call the replace() method multiple times on the same string.
# replacing multiple characters
# using chaining replace() method
str = "[email protected]"
# replace '@' with '_' & 'e' with 'E' & 'o' with '0'
str = str.replace("@", "_").replace("e", "E").replace("o", "0")
print(str) # HEll0_W0rld
HEll0_W0rld

# 2: replace multiple characters when map of characters is given
Suppose you have a dictionary of characters to be replaced and the characters to replace with.
- Use for loop to iterate over the list and get the key and value.
- Use replace() method to replace the character with the value.
# replace multiple characters
# when dictionary of characters is given
str = "[email protected]"
repl_map = {"@": "_", "e": "E", "o": "0"}
# loop through the map and access the key and value
for key, value in repl_map.items():
# use replace() method to replace the characters
str = str.replace(key, value)
print(str) # HEll0_W0rld
HEll0_W0rld
# 3: Replacing multiple characters when list of characters is given
Previously we have seen how to replace multiple characters when a map of characters is given. In a similar way, you may have a list of characters to be replaced and the characters to replace with.
Here are the steps to replace multiple characters when the list of characters is given.
- Use for loop to iterate over the list the list of tuples.
- Access the key and value and use replace() method to replace the character with the value.
# replace multiple characters
# when the list of characters is given
str = "[email protected]"
repl_list = [("@", "_"), ("e", "E"), ("o", "0")]
# loop through the list and access the key and value
for key, value in repl_list:
# use replace() method to replace the characters
str = str.replace(key, value)
print(str) # HEll0_W0rld
HEll0_W0rld
Conclusion
Here you have seen how to replace multiple characters in a string in python.
You can use the replace() method multiple times to replace multiple characters in a string or simply use the chaining method.
Another way is using a map or list of characters to be replaced and the characters to replace with.
Happy coding!😊