Remove Special Characters from String Python
The generic problem faced by the programmers is to remove unwanted characters from a string using Python. But sometimes the requirement is way above and demands the removal of more than 1 character, but a list of such malicious characters. These can be in the form of special characters for reconstructing valid passwords and many other applications possible. So our task is to remove the unwanted characters from the string.
Input: "Ge;ek * s:fo ! r;Ge * e*k:s !" Output: w3wiki Explanation: In This, we are removing special characters from string in Python.
Remove Special Characters from String in Python
Here we will explore different methods of removing special characters from strings in Python.
Python Remove Special Characters from String using str.isalnum()
Here we will Remove Special Characters from String Python using String isalnum() method checks whether all the characters in a given string are alphanumeric or not. It returns a boolean as True – If all the characters are alphanumeric or else false – If one or more characters are not alphanumeric.
Python3
string = "Ge;ek * s:fo ! r;Ge * e*k:s !" test_str = ''.join(letter for letter in string if letter.isalnum()) print (test_str) |
Output
w3wiki
Python Remove Special Characters from String using Replace()
Here we will Remove Special Characters from String Python using str.replace() inside a loop to check for a bad_char and then replace it with the empty string hence removing it. This is the most basic approach and inefficient on a performance point of view.
Python3
# initializing bad_chars_list bad_chars = [ ';' , ':' , '!' , "*" , " " ] # initializing test string test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !" # printing original string print ( "Original String : " + test_string) # using replace() to # remove bad_chars for i in bad_chars: test_string = test_string.replace(i, '') # printing resultant string print ( "Resultant list is : " + str (test_string)) |
Output
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : w3wiki
Remove a Specific Character from a String using join() + generator
Here we will Remove Special Characters from String Python using Python join() we remake the string. In the generator function, we specify the logic to ignore the characters in bad_chars and hence construct a new string free from bad characters.
Python3
# initializing bad_chars_list bad_chars = [ ';' , ':' , '!' , "*" , " " ] # initializing test string test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !" # printing original string print ( "Original String : " + test_string) # using join() + generator to # remove bad_chars test_string = ''.join(i for i in test_string if not i in bad_chars) # printing resultant string print ( "Resultant list is : " + str (test_string)) |
Output
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : w3wiki
Remove Special Characters from String using translate()
Here we will Remove Special Characters from String Python using translate(). The most elegant way to perform this particular task, this method is basically used for achieving the solution to this kind of problems itself, we can translate each bad_char to an empty string and get the filtered string.
Python3
import string # initializing bad_chars_list bad_chars = [ ';' , ':' , '!' , "*" ] # initializing test string test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !" # printing original string print ( "Original String : " + test_string) # using translate() to # remove bad_chars delete_dict = {sp_character: '' for sp_character in string.punctuation} delete_dict[ ' ' ] = '' table = str .maketrans(delete_dict) test_string = test_string.translate(table) # printing resultant string print ( "Resultant list is : " + str (test_string)) |
Output
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : w3wiki
Remove Special Characters from String using filter()
Here we will Remove Special Characters from String Python using Filter(). This is yet another solution to perform this task. Using the lambda function, filter function can remove all the bad_chars and return the wanted refined string.
Python3
# initializing bad_chars_list bad_chars = [ ';' , ':' , '!' , "*" ] # initializing test string test_string = "Ge;ek*s:fo!r;Ge*e*k:s!" # printing original string print ( "Original String : " + test_string) # using filter() to # remove bad_chars test_string = ''.join(( filter ( lambda i: i not in bad_chars, test_string))) # printing resultant string print ( "Resultant list is : " + str (test_string)) |
Output
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : w3wiki
Remove Special Characters from String using re.sub() function:
Here we will Remove Special Characters from String Python using Regular Expressions. Regular expressions are used to identify the bad character in the string and re.sub function is used to replace the bad_char from the string.
Python3
import re # initializing string test_str = "Ge;ek * s:fo ! r;Ge * e*k:s !" # Bad character list bad_char = [ ";" , "!" , "*" , ":" , " " ] # printing original string print ( "The original string is : " + test_str) # using re.sub() # remove bad_char from string temp = '' for i in bad_char: temp + = i res = re.sub(rf '[{temp}]' , '', test_str) # printing result print ( "The strings after extra space removal : " + str (res)) |
Output
The original string is : Ge;ek * s:fo ! r;Ge * e*k:s !
The strings after extra space removal : w3wiki
Remove Special Characters from String using in, not in operators
Here we will Remove Special Characters from String Python using In and Not In operators, used to check the presence of unwanted characters in the string.
Python3
# Python3 code to demonstrate # removal of bad_chars # initializing bad_chars_list bad_chars = [ ';' , ':' , '!' , "*" , " " ] # initializing test string test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !" # printing original string print ( "Original String : " + test_string) s = "" for i in test_string: if i not in bad_chars: s + = i # printing resultant string print ( "Resultant list is : " + str (s)) |
Output
The original string is : Ge;ek * s:fo ! r;Ge * e*k:s !
The strings after extra space removal : w3wiki
Remove Special Characters from String using Map and Lambda Function
Here we will Remove Special Characters from String Python using map and lambda functions. One approach that you can use to remove unwanted characters from a string is to use a combination of the map function and a lambda function. The map function applies a function to each element in an iterable, such as a list or string. The lambda function is a small anonymous function that can take in any number of arguments but only has one expression.
Python3
# Initialize list of unwanted characters bad_chars = [ ';' , ':' , '!' , "*" , " " ] # Initialize test string test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !" # Print original string print ( "Original String : " + test_string) # Use map and lambda function to remove unwanted characters filtered_string = ' '.join(map(lambda x: x if x not in bad_chars else ' ', test_string)) # Print filtered string print ( "Filtered String : " + filtered_string) #This code is contributed by Edula Vinay Kumar Reddy |
Output
The original string is : Ge;ek * s:fo ! r;Ge * e*k:s !
The strings after extra space removal : w3wiki
Contact Us