Remove special characters from a string in python

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

In Python the strings are immutable. It means we can not change its contents. But we can create a new string with only a few selected characters from the original string. Then we can assign back this new string to the original variable. It will give an effect that string has been modified and unwanted characters have been deleted from it.

Let’s see different ways to delete special characters from a string,

Remove special characters from a string using regex

In python, string.punctuation from string module contains all the special characters i.e.

r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""

We can use this to create a regex pattern, that will match all the special characters in a string. Then using the sub() function of regex module, we can replace all the special characters with an empty string. Let’s understand with an example,

import string
import re

sample_str = "Test&[88]%%$$$#$%-+String"

# Create a regex pattern to match all special characters in string
pattern = r'[' + string.punctuation + ']'

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

print(sample_str)

Output:

Test88String

It removed all the special characters from the string.

Another approach:

Instead of looking for special characters in string and removing them. We can look for all alphanumeric characters and remove the remaining characters. For example,

import re

sample_str = "Test&[88]%%$$$#$%-+String"

# Create a regex pattern to match all characters except letter or numbers
pattern = r'[^A-Za-z0-9]+'

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

print(sample_str)

Output:

Test88String

It also removed all the special characters from the string.

Remove special characters from a string using List comprehension and join()

Using list comprehension, iterate over all the characters of string one by one and skip characters non alphanumeric characters. It returns a list of filtered characters. Combine these remaining characters using join() and assign it back to same variable. It will give an effect that we have deleted all special characters from the string. For example,

sample_str = "Test&[88]%%$$$#$%-+String"

# Remove special characters from a string
sample_str = ''.join(item for item in sample_str if item.isalnum())

print(sample_str)

Output:

Test88String

It also removed all the spcecial characters from the string.

Remove special characters from a string using filter()

In Python, we can use the filter() function to filter out special characters from a string. Steps are as follows,

  • Along with the string to be modified, pass the isalpha() function to the filter() function, as the conditional argument.
  • filter() function loops through all characters of string and yields only those characters for which isalpha() function returns True i.e. all characters except the special characters.
  • 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 all special characters from the string.

For example,

sample_str = "Test&[88]%%$$$#$%-+String"

# Remove special characters from a string
sample_str = ''.join(filter(str.isalnum, sample_str))

print(sample_str)

Output:

Test88String

It also removed all the special characters from the string.

Remove special characters from a string using translate()

The string class in python, has 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 special character will be mapped to an empty string. For that we can use string.punctuation, that contains all the special characters and the maketrans() function that creates a translation table.

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

import string

sample_str = "Test&[88]%%$$$#$%-+String"

# Create translation table in which special charcters
# are mapped to empty string
translation_table = str.maketrans('', '', string.punctuation)

# Remove special characters from the string using translation table
sample_str = sample_str.translate(translation_table)

print(sample_str)

Output:

Test88String

It also removed all the spcecial characters from the string.

Summary:

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

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top