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.

Example
# replace multiple characters in a string

# 👉 Method 1
# chaining replace() method

str = "Hello@World"
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 = "Hello@World"

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.

Example
# replacing multiple characters
# using chaining replace() method

str = "Hello@World"

# replace '@' with '_' & 'e' with 'E' & 'o' with '0'
str = str.replace("@", "_").replace("e", "E").replace("o", "0")

print(str) # HEll0_W0rld
HEll0_W0rld
Python replace multiple character
Python replace multiple character

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

  1. Use for loop to iterate over the list and get the key and value.
  2. Use replace() method to replace the character with the value.
Example
# replace multiple characters
# when dictionary of characters is given

str = "Hello@World"
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.

  1. Use for loop to iterate over the list the list of tuples.
  2. Access the key and value and use replace() method to replace the character with the value.
Example
# replace multiple characters
# when the list of characters is given

str = "Hello@World"
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!😊