How to Replace Multiple Characters in a String in Python

  1. Use str.replace() to Replace Multiple Characters in Python
  2. Using re.sub() or re.subn() to Replace Multiple Characters in Python
  3. translate() and maketrans() to Replace Multiple Characters in Python

This tutorial shows you how to replace multiple characters in a string in Python.

Let’s say we want to remove special characters in a string and replace them with whitespace.

  • The list of special characters to be removed would be !#$%^&*().
  • Also, we want to replace commas , with whitespace.
  • The sample text that we will manipulate:
A!!!,Quick,brown#$,fox,ju%m%^ped,ov&er&),th(e*,lazy,d#!og$$$

Use str.replace() to Replace Multiple Characters in Python

We can use the replace() method of the str data type to replace substrings into a different output.

replace() accepts two parameters, the first parameter is the regex pattern you want to match strings with, and the second parameter is the replacement string for the matched strings.

It also a third optional parameter in replace() which accepts an integer to set the maximum count of replacements to execute. If you put 2 as a count parameter, the replace() function will only match and replace 2 instances within the string.

str.replace('hello', 'hi') will replace all instances of hello in a string with hi. If you have a string Hello World and run the replace function to it, it would become Hi World after execution.

Let’s use replace on the sample text that we declared above. First removing the special characters by looping each character and replacing them with an empty string, then converting commas into whitespace.

txt = "A!!!,Quick,brown#$,fox,ju%m%^ped,ov&er&),th(e*,lazy,d#!og$$$"

def processString(txt):
  specialChars = "!#$%^&*()" 
  for specialChar in specialChars:
    txt = txt.replace(specialChar, '')
  print(txt) # A,Quick,brown,fox,jumped,over,the,lazy,dog
  txt = txt.replace(',', ' ')
  print(txt) # A Quick brown fox jumped over the lazy dog  

That means anything within the square bracket of spChars will be replaced by an empty string using txt.replace(spChars, '').

The string result of the first replace() function would then be:

A,Quick,brown,fox,jumped,over,the,lazy,dog

The next replace() call will replace all instances of comma , into single whitespace:

A Quick brown fox jumped over the lazy dog

Using re.sub() or re.subn() to Replace Multiple Characters in Python

In Python, you can import the re module, which has an amount of expression matching operations for regex for you to utilize.

Two of such functions within re is sub() and subn().

Let’s declare another string example for these methods. Let’s say we want to replace all numbers within a string into X:

txt = "Hi, my phone number is 089992654231. I am 34 years old. I live in 221B Baker Street. I have 1,000,000 in my bank account."

re.sub() to Replace Multiple Characters in Python

The function has 3 main arguments. The first argument accepts a regex pattern, the second argument is a string to replace the matched patterns, and the third is the string to operate with.

Create a function convert all the numbers within a string to X.

import re

txt = "Hi, my phone number is 089992654231. I am 34 years old. I live in 221B Baker Street. I have 1,000,000 in my bank account."

def processString3(txt):
  txt = re.sub('[0-9]', 'X', txt)
  print(txt)
  
processString3(txt)

Output:

Hi, my phone number is XXXXXXXXXXXX. I am XX years old. I live in XXXB Baker Street. I have X,XXX,XXX in my bank account.

re.subn() to Replace Multiple Characters in Python

This function is essentially the same as re.sub() but instead returns a tuple of the converted string and the number of replacements made.

import re

txt = "Hi, my phone number is 089992654231. I am 34 years old. I live in 221B Baker Street. I have 1,000,000 in my bank account."

def processString4(txt):
  txt, n = re.subn('[0-9]', 'X', txt)
  print(txt)
  
processString4(txt)

Output:

Hi, my phone number is XXXXXXXXXXXX. I am XX years old. I live in XXXB Baker Street. I have X,XXX,XXX in my bank account.'
txt, n = re.subn('[0-9]', 'X', txt)

In the above code snippet, the processed string is assigned to txt and the replacement counter is assigned to n.

re.subn() is useful if you want to note how many pattern groups you manipulated as metrics or for further processing.

translate() and maketrans() to Replace Multiple Characters in Python

translate() and maketrans() use a different approach other than regex, it makes use of dictionaries to map old to new values.

maketrans() accepts 3 parameters or a single dictionary of mappings:

  • str1 - String of characters to be replaced
  • str2 - String of replacements for characters above
  • str3 - String of characters to be deleted

maketrans() a mapping table between the original string and its replacement.

translate() accepts whatever maketrans() returns and then generate the translated string.

Let’s say we want to convert all lowercase vowels within a string into uppercase and delete every x, y, and z found in the string.

txt = "Hi, my name is Mary. I like zebras and xylophones."

def processString5(txt):
  transTable = txt.maketrans("aeiou", "AEIOU", "xyz")
  txt = txt.translate(transTable)
  print(txt)
  
processString5(txt)

Output:

HI, m nAmE Is MAr. I lIkE EbrAs And lOphOnEs.

translate() converted all lowercase vowels into uppercase versions and removed all instances of x, y, and z.

One other approach to use these methods is to use a single dictionary of mappings instead of 3 arguments.

def processString6(txt):
  dictionary = {'a': 'A', 'e':'E', 'i': 'I', 'o': 'O', 'u': 'U', 'x': None, 'y': None, 'z': None}
  transTable = txt.maketrans(dictionary)
  txt = txt.translate(transTable)
  print(txt)

This will still produce the same output as processString5 but is implemented with dictionaries. You can use whatever is more convenient for you.

In summary, there are multiple ways you can replace multiple characters in a string by using built-in functions or functions from imported libraries in Python.

The most common method is to use replace(). re.sub() and subn() also are fairly easy to use and learn. translate() uses a different approach as it does not rely on regular expressions to perform string manipulation, instead it relies on dictionaries and maps.

If you want to, you can even manually loop over the string using for loops and add your own conditions to replace and just use substring() or split(), but it would be very inefficient and redundant. Python offers existing functions to do the job for you, which is much easier than doing the dirty work yourself.

Related Article - Python String

  • How to Convert Integer to String in Python
  • How to Convert String to Lowercase in Python 2 and 3