Python | Removing unwanted characters from string

30 Jun, 2022
The generic problem faced by the programmers is removing a character from the entire string. 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. Let’s discuss certain ways to perform this particular task.Method #1 : Using replace() One can use 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
# Python3 code to demonstrate# removal of bad_chars# using replace() # initializing bad_chars_listbad_chars = [';', ':', '!', "*", " "] # initializing test stringtest_string = "Ge;ek * s:fo ! r;Ge * e*k:s !" # printing original stringprint ("Original String : " + test_string) # using replace() to# remove bad_charsfor i in bad_chars :    test_string = test_string.replace(i, '') # printing resultant stringprint ("Resultant list is : " + str(test_string))
Output :  
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
Method #2 : Using join() + generator By using join() we remake the string. In generator function, we specify the logic to ignore the characters in bad_chars and hence constructing new string free from bad characters. 
Python3
# Python3 code to demonstrate# removal of bad_chars# using join() + generator # initializing bad_chars_listbad_chars = [';', ':', '!', "*", " "] # initializing test stringtest_string = "Ge;ek * s:fo ! r;Ge * e*k:s !" # printing original stringprint ("Original String : " + test_string) # using join() + generator to# remove bad_charstest_string = ''.join(i for i in test_string if not i in bad_chars) # printing resultant stringprint ("Resultant list is : " + str(test_string))
Output :  
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
Method #3 : Using translate() The most elegant way to perform this particular task, this method is basically used for achieving the solution to these kind of problems itself, we can translate each bad_char to empty string and get filtered string. 
Python3
# Python3 code to demonstrate# removal of bad_chars# using translate()import string# initializing bad_chars_listbad_chars = [';', ':', '!', "*"] # initializing test stringtest_string = "Ge;ek * s:fo ! r;Ge * e*k:s !" # printing original stringprint ("Original String : " + test_string) # using translate() to# remove bad_charsdelete_dict = {sp_character: '' for sp_character in string.punctuation}delete_dict[' '] = ''table = str.maketrans(delete_dict)test_string = test_string.translate(table) # printing resultant stringprint ("Resultant list is : " + str(test_string))
Output :  
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
Method #4 : 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
# Python3 code to demonstrate# removal of bad_chars# using filter() # initializing bad_chars_listbad_chars = [';', ':', '!', "*"] # initializing test stringtest_string = "Ge;ek*s:fo!r;Ge*e*k:s!" # printing original stringprint("Original String : " + test_string) # using filter() to# remove bad_charstest_string = ''.join((filter(lambda i: i not in bad_chars, test_string)))# printing resultant stringprint("Resultant list is : " + str(test_string))
Output :  
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
Method#5: Using re.sub() function: Regular expression are used to identify the bad character in string and re.sub function is used to replace the bad_char from the string. 
Python3
# Python3 code to demonstrate# removal of bad character# Using re.sub()import re # initializing stringtest_str = "Ge;ek * s:fo ! r;Ge * e*k:s !" # Bad character listbad_char = [";", "!", "*", ":", " "] # printing original stringprint("The original string is : " + test_str) # using re.sub()# remove bad_char from stringtemp = ''for i in bad_char:    temp+= i;res = re.sub(rf'[{temp}]', '', test_str) # printing resultprint("The strings after extra space removal : " + str(res))
The original string is : Ge;ek * s:fo ! r;Ge * e*k:s !
The strings after extra space removal : GeeksforGeeks
sagarjha
tester12345678
satyam00so
sagartomar9927
Python list-programs
Python string-programs
python-list
python-string
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.