Remove Special Characters from String Python

Here, we will develop a program to remove special characters from the string in Python. If the string was “[email protected]” then the result in the string will be “KnowProgram”. We will discuss how to remove all special characters from the given string using Regular Expression, replace(), translate(), join(), filter() method, and str.isalnum() function.

Python remove Special Characters from String

We will first be importing Regular Expression (RegEx module). The regular expression will automatically remove the special characters from the string. The regular expression for this will be [^a-zA-Z0-9], where ^ represents any character except the characters in the brackets.

# Python program to remove all special characters from string

# importing RegEx module
import re

# take string
string = input('Enter any string: ')
 
# using regular expression to remove special characters
new_string = re.sub(r'[^a-zA-Z0-9]','',string)

# print string without special characters
print('New string:', new_string)

Output:-

Enter any string: @Knowprogram*5
New string: Knowprogram5

Replace Special Characters in Python

The replace() method is a built-in functionality offered in Python. It replaces all the occurrences of the old substring with the new substring. One can use replace() inside a loop to check for a special_char and then replace it with the empty string hence removing it.

# Python program to remove all special characters from string

# take string
string = input('Enter any string: ')

# initializing special characters
special_char = '@_!#$%^&*()<>?/\|}{~:;[]'
 
# using replace () to remove special characters
for i in special_char:
    string = string.replace(i, '')

# print string without special characters
print('New string:', string)

Output:-

Enter any string: Py$th^on3%
New string: Python3

Remove Special Characters from String in Python

We are using the join() method to remove special characters. In the generator function, we specify the logic to ignore the characters in special_char and hence constructing a new string free from special characters.

# Python program to remove all special characters from string

# take string
string = input('Enter any string: ')

# initializing special characters
special_char = '@_!#$%^&*()<>?/\|}{~:;[]'
 
# using join() + generator to remove special characters
new_string = ''.join(x for x in string if not x in special_char)
 
# print string without special characters
print('New string:', new_string)

Output:-

Enter any string: join(special_char)
New string: joinspecialchar

Python clean String from Special Characters

In the previous program, we used the join() method but in this program, we are using the join(), filter(), and lambda() function to remove all special characters from a string. The filter() method constructs an iterator from elements of an iterable for which a function returns true.

# Python program to remove all special characters from string

# take string
string = input('Enter any string: ')

# initializing special characters
special_char = '@_!#$%^&*()<>?/\|}{~:;[]'
 
# using filter() to remove special characters
new_string = ''.join((filter(lambda i: i not in special_char, string)))

# print string without special characters
print('New string:', new_string)

Output:-

Enter any string: join(filter(lambda:)
New string: joinfilterlambda

Python replace Special Characters

This python program also performs the same task but in different ways. In this program, we are also using str.isalnum() function. The str.isalnum() method returns True if the characters are alphanumeric characters, meaning no special characters in the string. It will return False if there are any special characters in the string.

# Python program to remove all special characters from string

# take string
string = input('Enter any string: ')
 
# using str.isalnum() function to remove special characters
new_string = ''.join(filter(str.isalnum, string))

# print string without special characters
print('New string:', new_string)

Output:-

Enter any string: join(filter(str.isalnum, string))
New string: joinfilterstrisalnumstring

Remove all Special Characters from String Python

The translate() method returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table. Use the maketrans() method to create a mapping table. If a character is not specified in the dictionary/table, the character will not be replaced. We can translate each special_char to an empty string and get a filtered string.

# Python program to remove all special characters from string

# importing string function
import string

# take string
test_string = input('Enter any string: ')
 
# using translate() to remove special characters
new_string = test_string.translate(str.maketrans('', '', string.punctuation))

# print string without special characters
print('New string:', new_string)

Output:-

Enter any string: #string.translate()
New string: stringtranslate

Also See:- Check if String Starts with Vowel

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply