In this article, we will discuss four different ways to delete multiple characters from a string in python.

Suppose we have a string “A small sample String for testing” and a list of characters that need to be deleted from string i.e.

list_of_chars = ['s', 't', 'a', 'A', ' ']

Let’s see how to delete these characters from the string.

Delete multiple characters from string using translate() function

The string class in python, provides a function translate(). It accepts a translation table as an argument and replaces the characters in string based on the mapping in the translation table. We can create a translation table, where each character that we want to be deleted from string, will be mapped to an empty string. Like,

  • Ascii value of ‘s’ : ”
  • Ascii value of ‘t’ : ”
  • Ascii value of ‘a’ : ”
  • Ascii value of ‘A’ : ”
  • Ascii value of ‘ ‘ : ”

We will pass this translation table to translate() function as an argumment. Due to which translate() function will replace all the occurrences of these characters with an empty string. Basically it will remove all the occurrences of these characters from the string. For example,

sample_str = 'A small sample String for testing'

# A list containing multiple characters, that needs to be deleted from the string.
list_of_chars = ['s', 't', 'a', 'A', ' ']

# Create a mapping table to map the characters 
# to be deleted with empty string
translation_table = str.maketrans('', '', ''.join(list_of_chars))

# Remove multiple characters from the string
sample_str = sample_str.translate(translation_table)

print(sample_str)

Output:

mllmpleSringforeing

It removed all occurrences of multiple characters from the string.

Delete multiple characters from string using regex

In Python, the regex module provides a function to replace the contents of a string based on a matching regex pattern. Signature of function is like this,

sub(pattern, replacement_str, original_str)

We can use this to remove multiple characters from a string. For this we need to pass a regex pattern that matches all the occurrences of the given characters. Also, as a replacement string we need to pass a empty string. For example, let’s see how to delete characters ‘s’, ‘t’, ‘a’, ‘A’ and ‘ ‘ from a string using regex,

import re

sample_str = 'A small sample String for testing'

# A list containing multiple characters, that needs to be deleted from the string.
list_of_chars = ['s', 't', 'a', 'A', ' ']

# Create regex pattern to match all characters in list
pattern = '[' +  ''.join(list_of_chars) +  ']'

# Remove multiple characters from the string
sample_str = re.sub(pattern, '', sample_str)

print(sample_str)

Output:

mllmpleSringforeing

It removed all occurrences of ‘s’, ‘t’, ‘a’, ‘A’ and ‘ ‘ from the string.

Delete multiple characters from string using replace()

The string class provides a function to replace a sub-string in a string i.e.

str.replace(to_be_replaced, replacement)

It accepts two arguments i.e. the string to be replaced and the replacement string. It returns a copy of the calling string object but with the changed contents i.e. after replacing all the occurrences of sub-string to_be_replaced with the given replacement string. So, to delete multiple characters from a string using replace() function, follow this logic:

Iterate over all the characters to be deleted and for each character, pass it to the replace() function along with the empty string. For example,

sample_str = 'A small sample String for testing'

# A list containing multiple characters, that needs to be deleted from the string.
list_of_chars = ['s', 't', 'a', 'A', ' ']

# Remove multiple characters from the string
for character in list_of_chars:
    sample_str = sample_str.replace(character, '')

print(sample_str)

Output:

mllmpleSringforeing

It removed all occurrences of multiple characters i.e. ‘s’, ‘t’, ‘a’, ‘A’ and ‘ ‘ from the string.

Delete multiple characters from string using filter() and join()

In Python, you can use the filter() function to filter all the occurences of a characters from a string. Steps are as follows,

  • Create a lambda function, that accepts a character as an argument and returns True only if the passed character matches with any of the given the characters that need to be deleted.
  • Along with the string to be modified, pass the above created lambda function to the filter() function, as the conditional argument.
  • filter() function loops through all characters of string and yields only those characters for which lambda function returns True i.e. all characters except the characters that need to be deleted.
  • Use join() function to combine all yielded characters returned by filter() function.
  • Assign back the joined string returned by join() function to the original variable. It will give an effect that we have deleted multiple characters from the string.

For example,

sample_str = 'A small sample String for testing'

# A list containing multiple characters, that needs to be deleted from the string.
list_of_chars = ['s', 't', 'a', 'A', ' ']

# Filter multiple characters from string
filtered_chars = filter(lambda item: item not in list_of_chars, sample_str)

# Join remaining characters in the filtered list
sample_str = ''.join(filtered_chars)

print(sample_str)

Output:

mllmpleSringforeing

Summary:

We learned about different ways to delete multiple characters from a string in python.