Remove specific characters from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 19, 2022

banner

Photo from Unsplash

Remove specific characters from a String in Python #

Use the re.sub() method to remove specific characters from a string, e.g. new_str = re.sub(r'[!#@^]', '', my_str). The re.sub method will remove the specified characters from the string by replacing them with empty strings.

main.py
import re my_str = 'one !#two !#three @four ^five' # ✅ Remove specific characters from string (using re.sub()) new_str = re.sub(r'[!#@^]', '', my_str) print(new_str) # 👉️ 'one two three four five' # ---------------------------------------------------------- # ✅ Remove specific characters from string (using generator expression) new_str = ''.join(char for char in my_str if char not in '!#@^') print(new_str) # 👉️ 'one two three four five' # ---------------------------------------------------------- # ✅ Remove specific characters from string (using for loop) new_str = '' for char in my_str: if char not in '!#@^': new_str += char print(new_str) # 👉️ 'one two three four five'

The first example uses the re.sub() method to remove specific characters from a string.

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

main.py
import re my_str = 'one !#two !#three @four ^five' new_str = re.sub(r'[!#@^]', '', my_str) print(new_str) # 👉️ 'one two three four five'

If the pattern isn't found, the string is returned as is.

The first argument we passed to the re.sub() method is a regular expression.

The square brackets [] are used to indicate a set of characters.

All the characters between the square brackets are the ones we want to remove from the string.

The re.sub() method removes the specified characters by replacing each with an empty string.

Alternatively, you can use a generator expression.

Remove specific characters from a String using generator expression #

To remove specific characters from a string:

  1. Use a generator expression to iterate over the string.
  2. Check if each character is not one of the characters to be removed.
  3. Use the str.join() method to join the remaining characters into a string.
main.py
my_str = 'one !#two !#three @four ^five' new_str = ''.join(char for char in my_str if char not in '!#@^') print(new_str) # 👉️ 'one two three four five'

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 use the not in operator to check if the current character is NOT one of the characters we want to remove.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

x not in s returns the negation of x in s.
main.py
print('!' not in 'hello') # 👉️ True print('!' not in 'hello!') # 👉️ False

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

main.py
my_str = 'one !#two !#three @four ^five' new_str = ''.join(char for char in my_str if char not in '!#@^') print(new_str) # 👉️ 'one two three four five'

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 call the join() method on an empty string to join the remaining characters without a separator.

An alternative approach is to use a basic for loop.

Remove specific characters from a String using for loop #

To remove specific characters from a string:

  1. Declare a variable that stores an empty string.
  2. Use a for loop to iterate over the string.
  3. Check if each character is not one of the characters to be removed.
  4. If the condition is met, append the character to the string.
main.py
my_str = 'one !#two !#three @four ^five' new_str = '' for char in my_str: if char not in '!#@^': new_str += char print(new_str) # 👉️ 'one two three four five'

We used a for loop to iterate over the string.

On each iteration, we use the not in operator to check if the current character is not one of the characters we want to remove.

If the condition is met, we reassign the new_str variable to its current value plus the current character.

The += operator is a shorthand for new_str = new_str + char.

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.