In this article, we will discuss five different ways to remove all occurrences of a character from a string in python.

Strings are immutable in python, therefore once string objects are created, we can not modify it. But we can create a copy of the string with modified or filtered content and then assign it back to the original variable. It will give us effect that we have deleted the contents from the string. Let’s use this logic to delete all the occurrences of a character from a string in python.

Remove all instances of a character from a string using replace()

In python, the string class provides a member 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 replacement string.

To use the replace() function for deleting all instances of a character from a string, we need to pass the given character as first argument and an empty string as the second argument in the replace() function. For example,

sample_str = 'This is a sample string'

single_char = 's'
# Remove all instances of character 's' from the string
sample_str = sample_str.replace(single_char, "")

print(sample_str)

Output:

Thi i a ample tring

It removed all the instances of the character ‘s’ from the string.

Remove all instances of a character from a string using translate()

In Python, the string class provides a member 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 the character to be removed will be mapped to the empty string i.e. ”. Suppose we want to delete all the occurrences of character ‘s’ from the string, so its translation table will be like this,

Ascii number of ‘s’ : ”

We will pass this translation table to translate() function as an argument. Due to which translate() function will replace all the occurrences of character ‘s’ with empty string. Basically it will remove all the occurrences of character ‘s’ from the string. For example,

sample_str = 'This is a sample string'

single_char = 's'
# Remove all instances of character 's' from the string
sample_str = sample_str.translate({ord(single_char): None})

print(sample_str)

Output:

Thi i a ample tring

It removed all the instances of the character ‘s’ from the string.

Remove all instances of a character 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 all the instances of a character 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 all the occurrences of character ‘s’ from a string using regex,

import re

sample_str = 'This is a sample string'

single_char = 's'
# Remove all instances of character 's' from the string
sample_str = re.sub(single_char, '', sample_str)

print(sample_str)

Output:

Thi i a ample tring

It removed all the instances of the character ‘s’ from the string.

Remove all instances of a character from string using filter() and join()

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

  • Create a lambda function, that accepts a character as an argument and returns True if passed character matches the character to be deleted.
  • Pass this lambda function as the conditional argument to filter() function along with the string to be modified.
  • filter() function iterates over all characters of string and yields only those characters for which lambda function returns True i.e. all characters except the character to be deleted.
  • Use join() function to combine all yielded characters returned by filter() function.
  • Assign back the string returned by join() function to original variable. It will give an effect the we have deleted all occurrences of a given character from the string.

For example,

sample_str = 'This is a sample string'

# Remove all instances of character 's' from the string
single_char = 's'

# Create a list of all characters in string except the character 's' 
filtered_chars = filter(lambda ch: ch != single_char, sample_str)

# Join back the filtered characters in list to create a string 
sample_str = ''.join(filtered_chars)

print(sample_str)

Output:

Thi i a ample tring

It removed all the instances of the character ‘s’ from the string.

Remove all instances of a character from string using List comprehension and join()

Iterate over all characters of string using list comprehension and create a list of all characters of string except the character to be deleted. Then using the join() function, combine all the remaianing characters. Then assign back the string returned by join() function to the original variable. It will give an effect the we have deleted all occurrences of a given character from the string.

For example,

sample_str = 'This is a sample string'

single_char = 's'

# Remove all instances of character 's' from the string
sample_str = ''.join([ch for ch in sample_str if ch != single_char])

print(sample_str)

Output:

Thi i a ample tring

It removed all the instances of the character ‘s’ from the string.

Summary:

We learned about different ways to delete all occurrences of a character from a string in python.