Python: Replace multiple characters in a string

How to replace multiple characters in a string in Python ?

In this article we will discuss about different ways to replace multiple characters in a string.

Suppose we have a String variable named my_string. So let’s see how we can replace its letters.

my_string = "You are studying from BTech Geek"

Now, we want to replace 3 characters of it i.e

  • ‘u’ will be replaced with ‘w’
  • ‘s’ will be replaced with ‘p’
  • ‘t’ will be replaced with ‘z’

Method-1 : Replacing multiple characters in a string using the replace() method

In python, the String class provides an inbuilt method replace()  which can be used to replace old character to new character. It replaces all occurrences of that character.

For example if we want to replace ‘a’ in a string to ‘b’ then at all the positions where ‘a’ is present those positions will be replaced with ‘b’ in the string.

Syntax : string_var_name.replace(old char, new char)

Now let’s see the implementation of it.

#Program :

#string 
my_string = "You are studying from BTech Geek"
char_to_replace = {'u': 'w',
                   's': 'p',
                   't': 'z'}
# Iterating over all key-value pairs in the dictionary
for key, value in char_to_replace.items():
    # Replacing matched key character with value character in string
    my_string = my_string.replace(key, value)
print(my_string)
Output :
Yow are pzwdying from BTech Geek

Method-2 : Replace multiple characters in a string using the translate ()

In python, the String class also provides an inbuilt method translate()  which can also be used like replace() to replace old character to new character. It replaces all occurrences of that character.

#Program :

#string 
my_string = "You are studying from BTech Geek"
char_to_replace = {'u': 'w',
                   's': 'p',
                   't': 'z'}
# Iterating over all key-value pairs in the dictionary
for key, value in char_to_replace.items():
    # Replacing matched key character with value character in string
    my_string = my_string.translate(str.maketrans(char_to_replace))
print(my_string)
Output : 
Yow are pzwdying from BTech Geek

Method-3 : Replacing multiple characters in a string using regex

regex module (re) in python provides a function sub() with the help of which we can also replace multiple characters of the string. Just we need to pass the pattern that we want to string.

#Program :

import re
#string 
my_string = "You are studying from BTech Geek"
char_to_replace = {'u': 'w',
                   's': 'p',
                   't': 'z'}
# Iterating over all key-value pairs in the dictionary
for key, value in char_to_replace.items():
    # Replacing characeters
    my_string = re.sub("[ust]",
                       lambda x: char_to_replace[x.group(0)],
                       my_string)
print(my_string)
Output : 
Yow are pzwdying from BTech Geek

Method-4 : Replacing multiple characters in a string using for loop

We can also replace multiple string in a string using for loop. It will iterate over the string character by6 character. When a matching character is found then it will replace the character with new character and will add to the string. If no replacement is found for that character then it ill add the character to the string.

For this we have to take a new string variable as the newly created string will stored in that new string variable.

Let’s see the implementation of it.

#Program :

#string 
my_string = "You are studying from BTech Geek"
char_to_replace = {'u': 'w',
                   's': 'p',
                   't': 'z'}
# Iterating over all key-value pairs in the dictionary
result_str = ''
# Iterating over all characters in string
for element in my_string:
    # Checking if character is in dict as key
    if element in char_to_replace:
        # If yes then it add the value of that char from dict to the new string
        result_str += char_to_replace[element]
    else:
        # If not then add the character in new string
        result_str += element
print(result_str)
Output : 
Yow are pzwdying from BTech Geek

Leave a Comment