What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

5 Ways to Replace Multiple Characters in String in Python

  • Oct 10, 2022
  • 7 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Mradula Mittal
5 Ways to Replace Multiple Characters in String in Python

Do you know that Strings are immutable objects in Python? Their values, once declared, cannot be changed. Hence came the need to use different functions to be able to operate on strings. In this article, we will learn how to replace multiple characters in a string in python.

Before moving further, let's brush up a little on Strings:

What are Strings?

In Python, everything is an object. Strings are also Python objects, but they are generally defined as a sequence of characters enclosed between quotation marks ( either ' ' or " " ). These characters can be a-z, A-Z, 0-9, or special characters (like $, %) and white spaces (' ').

What do you mean by Replacing Multiple Characters in a String?

The need to replace a few characters (maybe one or more) from a string often emerges while dealing with problems. But the question is, what do you understand by replacing multiple characters in a string?

Strings have many operations to be performed on them. But when we say "replacing characters in a string in Python", the following cases come to mind:

  1. Replacing a character in a String with another character. For example, Let's say we have a string "Favtutor" and we want to replace the first occurrence of 't' with 'T'. Now, since we are only replacing 't' with another 'T', this is known as replacing a single character in a string.
  2. Replacing All occurrences of a character in a String with another character. Let's take the above example, say you want to replace both the occurrences of "t" in string "Favtutor" with 'T'. This will result in the string: "FavTuTor". Hence, this is replacing all occurrences of a character in a string. (You can always provide the number of occurrences, of that character, you want to replace).
  3. Replacing Multiple Characters in a String with the 'same' character. For example, You have a string "Hello world" and you want to replace the characters, say "l" and "o", with another character, say '!'. This will result in: "He!!! W!r!d". This is an example of replacing multiple characters in a string with a single (same) character.
  4. Replacing Multiple Characters in a String with Other Characters (different). For example, Let's take the statement, "Strings are immutable". You need to replace the characters, say ['r', 'm', 'i'], with, let's take ['R', '!', 'M'], in the respective order, i.e. 'r' replaced by 'R', "M" replaced by '!' and 'i' replaced by 'M'. Hence, it will result in "StRMngs aRe M!!utable". Sometimes, this group of multiple characters can also be termed as 'SubStrings'.
  5. Removing characters in a string. For example, let's consider the string "Fav Tutor". Now, let's say you need to remove the blank space (' ') between 'Fav' and 'Tutor'. You can remove this blank space by replacing it with (""). Hence, the output here will be: "FavTutor".

Here you can learn how to remove spaces from a string in python.

You must be wondering how we can make changes (here, replace characters) in the strings. Strings are immutable, aren't they?

Well, Strings are immutable! The methods (you'll know them soon) take the original string as input and returns the changed (after replacing characters) string as output. This output is stored in another string variable. Hence, we need to have another variable (string object) to store the obtained output. Thus, no changes are made to the original string, retaining its immutable property.

How to Replace Multiple Characters in a String in Python?

Python offers different modules and methods to replace multiple characters in a string. Now, these multiple characters can be either separate or together as a 'word'. While we are focusing on replacing multiple characters, these methods can also be applied to replace a single character in a string in Python. 

Let's get started!

01) Using replace() method

Python offers replace() method to deal with replacing characters (single or multiple) in a string. The replace method returns a new object (string) replacing specified fields (characters) with new values. 

The replace() method takes two parameters as input, the first is the pattern you want (to match in the string) to be replaced and the second parameter is the pattern (character) you want to replace with. 

The most used syntax for replace() is:

newObject = originalString.replace('character to replace', 'character to be replaced with' )

Let's take an example:

# Using replace() method
string = "Bubblegum"

# replacing the 'b' character with 'B'
changed_string = string.replace("b", "B")

print("Original string: ", string)
print("New string: ", changed_string)

 

Output: 

Original string:  Bubblegum
New string:  BuBBlegum

 

Note how the 'b's in the original string 'Bubblegum' are replaced with 'B's. This is an example of replacing multiple occurrences of a single character in a string.

The replace() method also has a third parameter (optional) where you can mention the number of replacements you want to perform.

That is, if any word, for example, "Bubblegum" has more than one occurrence of a character, here 'b', then we can input the third parameter as 1, in case we need to only replace one 'b' from the string.

Hence, the basic syntax for replace() method is:

newObject = originalString.replace('character to replace', 'character to be replaced with, count of replacements to perform)

Let's take a look at the code for this example:

# Using replace() method
string = "Bubblegum"

# replacing the 'b' character with 'B'
changed_string = string.replace("b", "B", 1)

print("Original string: ", string)
print("New string: ", changed_string)

 

By default, all the occurrences of the first parameter are replaced in the string.

Output:

Original string:  Bubblegum
New string:  BuBblegum

 

Notice how only the first occurrence of 'b' was replaced by 'B'. This happened because the count of replacements to perform was set to 1.

02) Using replace() with Lists

In the above method, we have seen the use of replace() method for replacing single characters (single or more occurrences) with another character. The replace() method can also be used to replace multiple (different) characters with another (same or different for each) character.

Using the for loop: The most common way of working with lists is using the for a loop. To replace multiple characters (let's say different) using replace() method, you can list out all the characters, to be removed, in a list. 

An example of how to declare the list:

# multiple characters to be replace

string = "FavTutor Blog: How to Remove multiple characters in a string in Python"
# let's say we need to replace characters - 't', 'l', 'r'
# creating a list for the characters to be replaced
char_remov = ['t', 'l', 'r']

 

You might be wondering, why are we creating a list or even using a for a loop when we can directly call replace() on each of the characters.

Pretty simple, right?

No, it's not. You'll find the reason later in the blog. (So, keep an eye out for it!)

After initializing the list, you can either replace the mentioned characters with the same character (i.e. all by one) or with multiple characters (i.e. a different character for each).

Case: Replacing multiple characters with the same character:

# multiple characters to be replace
string = "FavTutor Blog: How to Remove multiple characters in a string in Python"
# let's say we need to replace characters - 't', 'l', 'r'
# creating a list for the characters to be replaced
char_remov = ["t", "l", "r"]

print("Original string: " + string)
# let's say we need to replace them with a special character '#'
# Using the for loop for each character of char_remov

for char in char_remov:
    # replace() "returns" an altered string
    string = string.replace(char, "#")

print("Altered string: " + string)

 

You'll notice how I have used a for loop to parse over the string for 'each character' in the list, to be replaced by another character (like "#"). 

Output:

Original string: FavTutor Blog: How to Remove multiple characters in a string in Python
Altered string: FavTu#o# B#og: How #o Remove mu##ip#e cha#ac#e#s in a s##ing in Py#hon

 

Once again, note that we have replaced the characters to be replaced (elements in list char_remov) with the same character ('#') above.

03) Using replace() with Dictionary

We can also use different characters for each character to be replaced. In order to achieve this, you must be aware of dictionaries!

What is a Dictionary in Python? a Dictionary is a collection of key-value pair entities. It is another data type offered by Python, also considered Python's implementation of associative array data structure. These are mutable and do not allow any duplicates since a key can only have one value.

A Dictionary is represented by: {}.

An example of a dictionary can be seen as:

# example to declare a dictionary
# dictionary = {'key' : value}
# method 1
dictionary = {"FavTutor": "blogs", "language": "Python"}

# accessing dictionary values
value1 = dictionary.get("FavTutor")
print("Values: ", value1, " in ", dictionary.get("language"))

 

Above is one of the methods to declare a dictionary. It also shows one of the approaches to obtaining values with the help of keys.

Output:

Values:  blogs  in  Python

 

Now that you know what is a dictionary and how it is used, let's get back to replacing characters in a string. Let's consider the same example used before but replace each character with a different character instead. (Let's try replacing them with their uppercase form. This will be easy to notice.)

Case: Replacing multiple characters with different characters in a string in Python

# multiple characters to be replaced
string = "Favtutor Blog: How to Remove multiple characters in a string in Python"
# characters to be replaced - 't', 'l', 'r', 's'
# characters to replace with -'T', 'L', 'R', 'S'

print("Original string: ", string)

""" creating a dictionary to replace characters
key - character to be replaced
value - character to replace with """

to_remov = {"t": "T", "l": "L", "r": "R", "s": "S"}

# .keys() returns a list of all the keys in the dictionary
for char in to_remov.keys():
    # dictionaryName[keys] - another way to get value
    string = string.replace(char, to_remov[char])

print("Altered string: " + string)

 

The use of a dictionary has made it easy to change each character to a different character with the help of key-value pair. Although you can use a dictionary to replace multiple characters with a single character, it's use is discouraged to do so (since that'll be unnecessary).

Output:

Original string:  Favtutor Blog: How to Remove multiple characters in a string in Python
Altered string: FavTuToR BLog: How To Remove muLTipLe chaRacTeRS in a STRing in PyThon

 

I hope you've found the answer to our question: 'Why do we need to use a loop?'

Well, since we need to replace multiple characters in a string, the need for a loop is really evident. We can call upon the replace() method for each character to be removed, but how long are you going to repeat the process? And in case of a large number of characters to be replaced, calling replace() on each character not only increases the length of your code but also affects its readability.

Also, it is more difficult to keep a track of a long list of characters to be replaced. Hence, by listing out (or creating a dictionary) for the characters to be replaced, it is easy for us to end a long repeating process with the help of a loop.

Keep that in mind. It's pretty basic but can often occur in interviews!

The replace() method is the most common method to replace any character in a string. You can use any of the versions above to replace multiple characters in a string in Python. There are special symbols (like '|') that can also be used with the expressions. This use of special characters or patterns to replace is involved in regular expressions (or RegEx).

04) Using re module

Python offers 're module' to provide ease while working with Regular expressions. You must know that regular expressions are a sequence of characters that forms a search pattern. Hence, regular expressions are basically strings. You can also replace multiple characters in a string with the help of the 're module'.

There are basically two ways of replacing the characters:

  1. Using the sub() function with meta characters or special sequence
  2. Using the sub() function with lists and dictionaries

Before moving on to these methods, let's learn about sub() function.

The sub() function in re module: The re module in Python offers a sub() function which replaces the matched character (the character given in the string) with the given (new) character. This returns a string with the new characters in place. Note that you need to import the re module in order to use sub() function.

The basic syntax for sub() function is:

newString = re.sub( 'characterToBeReplaced', 'characterToReplaceWith', stringName )

Let's take an example to understand how sub() function works:

Case: Replacing a single character with another character in a string

# import the regex module
import re

string = "Favtutor"
print("Original string: ", string)
"""Character to be replaced = 't',
character to replace with = 't'. """

# calling sub() function
newString = re.sub("t", "T", string)

print("The new string: ", newString)

 

The 't' here is also considered a regular expression. Also, note that, unlike the replace() method, the string name is passed as an input parameter to the sub function.

Output:

Original string:  Favtutor
The new string:  FavTuTor

 

The sub() function searches the string for the pattern (character) and then replaced the match (pattern) with the new character. It returns a new string with the changes made. Note that you can also specify the number of replacements to occur, by passing a "count" parameter at the end. Hence, the basic syntax of sub will be:

newString = re.sub('charactersToBeReplaced', 'characterToReplaceWith' , stringName, count )

Now, let's move on to replacing multiple characters using sub() function, with the help of special characters!

Recall that meta characters are characters with special meaning (for example - '|' for 'either or') and special sequences like '\s' for white spaces. These meta characters or special sequences can be used to replace multiple characters in a string.

Let's take note of the example below:

Case: Replacing multiple characters in a string with a single character, using special sequence and meta characters

# importing regex module
import re

# multiple characters to be replaced
string = "A master of all is a master of none"
print("Original string: ", string)
# let's say - characters to be replaced - 'm', 'n' and white spaces
# character to replace with '#'

# calling sub() function
# white space represented by special sequence- '\s'
# '|' used for either or

string = re.sub("m|n|\s", "#", string)
print("New string: ", string)

 

Note that I've used a special sequence ('\s') as well as a meta character ('|') in the above example.

Output:

Original String:  A master of all is a master of none
New string:  A##aster#of#all#is#a##aster#of##o#e

 

This is another way of replacing multiple characters using sub() function. But this way can be used till the number of characters to be replaced is restricted to only a few. Just like before, we will need to use a loop in order to work with replacing multiple characters (more than few). The sub() function can also be used with lists as well as dictionaries. 

Let's take an example of using sub() function with a dictionary:

Case: Replacing multiple characters in a string with different characters using a dictionary

# importing regex module
import re

# multiple characters to be replaced
string = "Favtutor Blog: How to Remove multiple characters in a string in Python"
# characters to be replaced - 't', 'l', 'r', 's'
# characters to replace with -'T', 'L', 'R', 'S'

print("Original string: ", string)

""" creating a dictionary to replace characters
key - character to be replaced
value - character to replace with """

to_remov = {"t": "T", "l": "L", "r": "R", "s": "S"}

# .keys() returns a list of all the keys in the dictionary
for char in to_remov.keys():
    # dictionaryName[keys] - another way to get value
    # calling out the sub function of re module
    string = re.sub(char, to_remov[char], string)

print("Altered string: " + string)

 

The sequence of characters (referenced with variable names 'char' and values in dictionary) are regular expressions. 

Output:

Original string:  Favtutor Blog: How to Remove multiple characters in a string in Python
Altered string: FavTuToR BLog: How To Remove muLTipLe chaRacTeRS in a STRing in PyThon

 

Apart from the sub() function, the re module also offers a subn() function, similar to the sub() function. The only difference is that the subn() function also provides the number of replacements it made in the string. Also, the sub() function can also be used with the "lambda" function.

You should try using the functions above. They might get you to your desired output.

05) Using translate() and maketrans()

The methods discussed so far involve regular expressions and can be used with lists and dictionaries. However, Python offers another method that uses dictionaries to map old values to new values, and hence to replace multiple characters in a string.

The maketrans() method generates a mapping table (dictionary) between the original character and its replacement. See the example below:

# original string
string = "Hi, my name is mradula mittal. i'm a machine learning enthusiast."
# calling maketrans() method
# replacing 'mi' with 'MI'
transTable = string.maketrans("mi", "MI")

# printing out output returned by maketrans()
print(transTable)

 

The maketrans() method creates a table (dictionary) mapping the old values with the new values.

Output:

{109: 77, 105: 73}

 

Note that the dictionary (returned by maketrans()) holds the ASCII values of the characters, (both, to be replaced and to replace with).

The translate() function accepts the table created by maketrans() and generates the translated string. Take a look at the below example to replace multiple characters in a string in Python:

# original string
string = "Hi, my name is mradula mittal. i'm a machine learning enthusiast."
# calling maketrans() method
# replacing 'mi' with 'MI'
translateTable = string.maketrans("mi", "MI")
output = string.translate(translateTable)

print("Original string: ", string)
print('Output: ', output)

 

Output:

Original string:  Hi, my name is mradula mittal. i'm a machine learning enthusiast.
Output:  HI, My naMe Is Mradula MIttal. I'M a MachIne learnIng enthusIast. 

 

This is another example of replacing multiple characters in a string using a dictionary. You can use the above method for any of the cases mentioned above.

Comparison between translate() and replace() method

The translate() method and replace() method, both are in-built functions in Python, to replace multiple characters in a string. You can use either of them to perform the task. Here's a comparison of both methods:

1. The translate() method replaces only single characters with arbitrary strings whereas the replace() method allows replacing strings with arbitrary lengths. For example:

# original string
string = "Hi, my name is mradula mittal. i'm a machine learning enthusiast."
# calling maketrans() method
# replacing 'mit' with 'MI'
translateTable = string.maketrans("mit", "MI")
output = string.translate(translateTable)

print("Original string: ", string)
print("Output: ", output)

 

Output:

Traceback (most recent call last):
  File ".\temp.py", line 5, in <module>
    translateTable = string.maketrans("mit", "MI")
ValueError: the first two maketrans arguments must have equal length

 

Note the error obtained on calling the maketrans() method. This error occurred due to the difference in length of the characters (or number of characters) to be replaced and the characters to be replaced with. Now, let's take a look at how replace() method works with the case:

# original string
string = "Hi, my name is mradula mittal. i'm a machine learning enthusiast."
# calling maketrans() method
# replacing 'mit' with 'MI'
output = string.replace("mit", "M")

print("Original string: ", string)
print("Output: ", output)

 

The replace() method treats the input parameter 'mit' as a string as a whole rather than treating it as a sequence of individual characters. Also, the arbitrary length of the parameters need not be same.

Output:

Original string:  Hi, my name is mradula mittal. i'm a machine learning enthusiast.
Output:  Hi, my name is mradula Mtal. i'm a machine learning enthusiast.    

 

Hence, you'll notice that rather than replacing 'm', 'i', 't', the replace() method has replaced only the combination of 'mit' with 'M'.

2. The translate() method can perform replacements of "multiple characters" at a single call while the replace() method can only replace a "single string" at once.

3. The translate() method can be used when you are not sure whether the new characters (characters to be replaced with) are also being replaced.

Difficult? Don't worry, let's take an example to understand the above comparisons.

For instance, consider a string, say 'abcd'. Now, you need to replace the characters 'a' with 'b', 'b' with 'c', and 'c' with 'd'.

In short, input = 'abcd'       and          expected output = 'bcdd'

Let's compare both methods on the above input:

# original string
original = "abcd"
# replacing 'a' -> 'b' , 'b' -> 'c', 'c' -> 'd'

# calling maketrans() method
translateTable = original.maketrans("abc", "bcd")
output = original.translate(translateTable)

print("Original string: ", original)
print("Using translate() method - Output: ", output)

# calling replace() method 
# using nested replace method
output = original.replace('a', 'b').replace('b', 'c').replace('c', 'd') # this is similar to using for loop print("Original string: ", original) print("Using replace() method - Output: ", output)

 

Note the difference between the outputs returned by both methods.

Output:

Original string:  abcd
Using translate() method - Output:  bcdd
Original string:  abcd
Using replace() method - Output:  dddd

 

Both methods produced different outputs! You must be wondering "how", right?

This occurred because the replace() method was being performed in a loop, one after the another. Let's list out the changes for a better understanding:

a. Input = 'abcd'

b. Replace 'a' with 'b' => string = 'bbcd'

c. Replace 'b' with 'c' => string = 'cccd'

d. Replace 'c' with 'd' => 'dddd'

Hence the output = 'dddd'

Thus, in this case, the translate() method meets produces the expected outcome.

Hence, on comparing both methods, you'll notice that you should use the translate() method when it comes to replacing multiple characters as it increases functionality. Although the replace() method has its own benefits in time optimization.

Also, check how to replace a single character in a string in python.

Conclusion

In this article, we looked at how to use Python's built-in functions to replace multiple characters in a string. This task can also be performed by using different variations of loops over the string. We have also compared the methods with different use cases. I hope this article helps you find a suitable method for your code. Happy coding!

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Mradula Mittal
I'm Mradula Mittal, a tech community enthusiast with a knack for content writing. Always eager to learn more about Python and Machine Learning. Having an analytical brain, I'm particular about my research and writing. I live by the motto "the more you share, the more you learn" and being a techie, it's fun to learn, create and share.