How to remove all special characters, punctuation and spaces from a string in Python?


In this article, we are going to find out how to remove all special characters punctuation and spaces from a string in Python.

The first approach is by using isalnum() method on each character of the string by iterating it using for loop. We will check if each character is either alphabet or number using isalnum(), if they are not then they will be removed else we will go on to next character.

If every character in the string is an alphanumeric character, the isalnum() method returns True (either alphabets or numbers). If not, False is returned.

Example

In the example given below, we are taking a string as input and we are removing whitespaces and special characters using isalnum() and for loop and printing the resultant string 

str1 = "Welcome #@ !! to Tutorialspoint123"

print("The given string is")
print(str1)

print("Removing special characters and white spaces")
print(''.join(i for i in str1 if i.isalnum()))

Output

The output of the above example is as follows −

The given string is
Welcome #@ !! to Tutorialspoint123
Removing special characters and white spaces
WelcometoTutorialspoint123

Using filter() and isalnum() method

The second approach is by using filter() and isalnum(). This approach is similar to the first one, but instead of using for loop and if statement we will be using filter() and we will use isalnum() for checking if the given character is alphabet or numeric.

Example

In the example given below, we are taking a string as input and we are removing all the whitespaces and special characters using filter() and isalnum() and are printing the resultant string 

str1 = "Welcome #@ !! to Tutorialspoint123"

print("The given string is")
print(str1)

print("Removing special characters and white spaces")
print(''.join(filter(str.isalnum, str1)))

Output

The output of the above example is as shown below −

The given string is
Welcome #@ !! to Tutorialspoint123
Removing special characters and white spaces
WelcometoTutorialspoint123

Using Regular Expressions

Regular expressions are used in the second technique. Import the re library and install it if it isn't already installed to use it. After importing the re library, we can use the regular expression "[A-Za-z0-9]+". The special characters and whitespaces will be replaced with whitespaces using the re.sub technique.

Example 

In the example given below, we are taking a string as input and we are removing all the special characters and whitespaces using regular expressions and are printing the resultant string 

import re
str1 = "Welcome #@ !! to Tutorialspoint123"

print("The given string is")
print(str1)

print("Removing special characters and white spaces")
print(re.sub('[^A-Za-z0-9]+', '', str1))

Output

The output of the above example is given below 

The given string is
Welcome #@ !! to Tutorialspoint123
Removing special characters and white spaces
WelcometoTutorialspoint123

Updated on: 07-Dec-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements