Python | Removing unwanted characters from string
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 that 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. Lets 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 performance point of view.
# Python3 code to demonstrate # removal of bad_chars # using replace() # 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 : 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 code to demonstrate # removal of bad_chars # using join() + generator # 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 : 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 code to demonstrate # removal of bad_chars # using translate() # 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 test_string = test_string.translate(None, ''.join(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 : 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 code to demonstrate # removal of bad_chars # using filter() # 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 = 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 : GeeksforGeeks
Recommended Posts:
- Python | Remove unwanted spaces from string
- Python program for removing i-th character from a string
- Python | Removing Initial word from string
- Python | Extract only characters from given string
- Iterate over characters of a string in Python
- Check if both halves of the string have same set of characters in Python
- Python | Check if frequencies of all characters of a string are different
- Python | Ways to check string contain all same characters
- Python | Return lowercase characters from given string
- Concatenated string with uncommon characters in Python
- Python | Split string into list of characters
- Python | Convert a list of characters into a string
- Python | Splitting string to list of characters
- Python | Splitting string to list of characters
- Python Dictionary to find mirror characters in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



