Remove all occurrences of character from String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 14, 2022

banner

Photo from Unsplash

Remove all occurrences of character from String in Python #

Use the str.replace() method to remove all occurrences of a character from a string, e.g. result = my_str.replace(char, ''). The replace() method will remove all occurrences of the character by replacing each occurrence with an empty string.

main.py
my_str = 'hello' result = my_str.replace('l', '') print(result) # 👉️ 'heo'

We used the str.replace() method to remove all occurrences of the l character from the string hello.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

You can also use this approach to remove all occurrences of multiple, consecutive characters from the string.

main.py
my_str = 'hello hello' result = my_str.replace('el', '') print(result) # 👉️ 'hlo hlo'

Alternatively, you can use a generator expression.

To remove all occurrences of a character from a string:

  1. Use a generator expression to iterate over the string.
  2. On each iteration, check if the current character is not equal to the character to be removed.
  3. Use the str.join() method to join the remaining characters into a string.
main.py
my_str = 'hello' result = "".join(char for char in my_str if char != 'l') print(result) # 👉️ 'heo'

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current character is not equal to l and return the result.

main.py
my_str = 'hello' # 👇️ ['h', 'e', 'o'] print(list(char for char in my_str if char != 'l'))

The last step is to use the str.join() method to join the remaining characters into a string.

main.py
my_str = 'hello' result = "".join(char for char in my_str if char != 'l') print(result) # 👉️ 'heo'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

For our purposes, we called the method on an empty string to join the remaining characters without a separator.

Alternatively, you can use the filter() function.

To remove all occurrences of a character from a string:

  1. Use the filter() function to filter out all occurrences of the character.
  2. Use the str.join() method to join the remaining characters into a string.
  3. The new string won't contain the specified character.
main.py
my_str = 'hello' result = ''.join(filter(lambda char: char != 'l', my_str)) print(result) # 👉️ 'heo'

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

The first argument we passed to filter is a lambda function that checks if the passed in character is not equal to l and returns the result.

main.py
my_str = 'hello' # 👇️ ['h', 'e', 'o'] print(list(filter(lambda char: char != 'l', my_str)))

The filter function will return a filter object that doesn't contain the specified character.

The last step is to join the remaining characters with the str.join() method.

main.py
my_str = 'hello' result = ''.join(filter(lambda char: char != 'l', my_str)) print(result) # 👉️ 'heo'
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.