Remove Special Characters From a String in Python

Recently, I was working on cleaning up some text data for a project in the USA, where customer feedback was collected through multiple online forms.

The issue? Many users typed in special characters like @, #, !, and %, which made the data messy and hard to analyze. But I noticed that I still needed to keep the spaces intact so that sentences remained readable.

In this tutorial, I’ll show you multiple ways to remove special characters from a string in Python but keep the spaces untouched. I’ll also share my firsthand experience with each method so you can decide which one works best for your project.

Method 1: Use Python re.sub() Function

The sub() function from the re module in Python allows us to replace occurrences of a pattern in a string with another string.

Code:

import re

def remove_special_characters(text):
    return re.sub(r'[^a-zA-Z0-9\s]', '', text)

text = "Welcome! To the USA."
cleaned_text = remove_special_characters(text)
print("Cleaned Text:", cleaned_text)

Output

Cleaned Text: Welcome To the USA

You can refer to the screenshot below to see the output.

remove special characters except for space from a string in Python

We can leverage this to remove special characters except for spaces by replacing them with an empty string.

Method 2: Use str.isalnum() Method in Python

The str.isalnum() method in Python checks if all string characters are alphanumeric (alphabets or numbers). We can iterate through each character in the string, retaining only alphanumeric characters and spaces.

Code:

def remove_special_characters(text):
    return ''.join(char for char in text if char.isalnum() or char.isspace())

text = "The Grand #Canyon—a bucket_ list must- see!"
cleaned_text = remove_special_characters(text)
print("Cleaned Text:", cleaned_text)

Output

Cleaned Text: The Grand Canyona bucket list must see

You can refer to the screenshot below to see the output.

How to remove special characters except space from a string in python

This method uses str.isalnum() and str.isspace() to efficiently remove all special characters while keeping only letters, numbers, and spaces.

Method 3: Use Python’s re.split() Function

The re.split() function splits a string based on a specified pattern in Python. We can utilize this function to split the string into substrings containing only alphanumeric characters and spaces, effectively removing special characters.

Here’s a simple example:

Code:

import re

def remove_special_characters(text):
    return ' '.join(re.split(r'\W+', text))

text = "Hot dogs ,and baseball at @Yankee Stadium—perfect summer nights!"
cleaned_text = remove_special_characters(text)
print("Cleaned Text:", cleaned_text)

Output

Cleaned Text: Hot dogs and baseball at Yankee Stadium perfect summer nights 

You can refer to the screenshot below to see the output.

How to remove special characters from string Python

This method uses re.split(r’\W+’, text) to split text by non-alphanumeric characters and then rejoins it, effectively removing special characters.

Method 4: Use replace() Method in Python

The replace() method in Python allows us to replace occurrences of a specified substring with another.

Code:

def remove_special_characters(text):
    special_chars = "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
    for char in special_chars:
        text = text.replace(char, '')

    return text

text = "@Golden Gate Bridge #offers stunning San -Francisco views!"
cleaned_text = remove_special_characters(text)
print("Cleaned Text:", cleaned_text)

Output

Cleaned Text: Golden Gate Bridge offers stunning San Francisco views

You can refer to the screenshot below to see the output.

Remove special characters from a string except space in Python

This method removes special characters by iterating through a list of symbols and replacing each one with an empty string using replace() in Python.

Method 5: Use join + generator in Python

Combining the join() method with a generator in Python allows us to create a new string consisting of only alphanumeric characters and spaces, effectively removing special characters.

Let’s see an example:

Code:

to_remove = [';', ':', '!', "*","@","."]
text = "Visit the Smithsonian Museum in @D.C. for !American history treasures!"
print("Original String : " + text)
text = ''.join(i for i in text if not i in to_remove)
print("Cleaned Data : " + str(text))

Output:

Original String : Visit the Smithsonian Museum in @D.C. for !American history treasures!
Cleaned Data : Visit the Smithsonian Museum in DC for American history treasures

You can refer to the screenshot below to see the output.

How to remove special characters from string except space in Python

This method filters out unwanted symbols using a generator and join(), creating a clean string with only the desired characters.

Method 6: Use Python’s findall() Function

The findall() function from the re module returns all non-repeated matches of a pattern in a string as a list of strings in Python. This function can find all alphanumeric substrings and spaces in the string.

Code:

import re

def remove_special_characters(text):
    return ''.join(re.findall(r'[a-zA-Z0-9\s]', text))

text = "Route '66—th' ultimate #American road trip!"
cleaned_text = remove_special_characters(text)
print("Cleaned Text:", cleaned_text)

Output

Cleaned Text: Route 66th ultimate American road trip

You can refer to the screenshot below to see the output.

remove special characters from string python

This method uses re.findall() to extract only alphanumeric characters and spaces, effectively removing all special characters from the string.

Method 7: Use translate() Method in Pyhton

The translate() method in Python maps one set of characters to another. It creates a translation table where each character in the first argument is mapped to the corresponding character in the second argument in Python.

Code:

def remove_special_characters(text):
    translation_table = str.maketrans('', '', "!@#$%^&*()_+<>?:{}|~`")
    cleaned_text = text.translate(translation_table)
    return cleaned_text

text = "Mount #Rushmore showcases $four iconic @presidents!"

cleaned_text = remove_special_characters(text)
print("Cleaned Text:", cleaned_text)

Output

Cleaned Text: Mount Rushmore showcases four iconic presidents

You can refer to the screenshot below to see the output.

How to Remove All Special Characters from a String except space in Python

This method uses str.translate() with a translation table to efficiently remove specified special characters from the string.

Method 8: Use filter() Method in Python

We can use the filter() method, which is a built-in method of Python, and it creates an iterator that retains only alphanumeric characters and spaces from a string.

Code:

def remove_special_characters(text):
    return ''.join(filter(lambda char: char.isalnum() or char.isspace(), text))

text = "Welcome! To the (USA)."
cleaned_text = remove_special_characters(text)
print("Cleaned Text:", cleaned_text)

Output

Cleaned Text: Welcome To the USA

You can refer to the screenshot below to see the output.

Python Remove Special Characters from a String

This method uses filter() with a lambda function to keep only alphanumeric characters and spaces, removing all special characters from the string.

Method 9: Use Map and Lambda Function

Python’s map() function applies a function to each element of an iterable, producing a new iterable. Also, we are using the lambda function with map() to iterate over each character in the input string.

Code:

def remove_special_characters(text):
    special_chars = "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
    cleaned_text = ''.join(map(lambda char: '' if char in special_chars else char, text))
    return cleaned_text

text = "Fireworks on the '@Fourth of July light' up the sky with '#bursts' of red, white, and blue!"
cleaned_text = remove_special_characters(text)
print("Cleaned Text:", cleaned_text)

Output

Cleaned Text: Fireworks on the Fourth of July light up the sky with bursts of red white and blue

You can refer to the screenshot below to see the output.

How to remove special characters but not spaces in Python

This method uses map() with a lambda function to remove special characters by replacing them with an empty string, keeping only the desired characters.

Conclusion

I explained 9 easy ways to remove special characters except for spaces from a string in Python. These methods are useful for data cleaning, ensuring that only relevant information remains for analysis or processing.

I explored techniques such as using regular expressions with re.sub(), re.split(), and findall() functions, and string methods like replace() and isalnum().

I can also combine the join() method with generator expressions and filter() and map() with lambda functions, and I efficiently achieved our goal to remove special characters except space from a string in Python.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.