
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing Unwanted Characters from String in Python
Python is a very commonly used program used for different purposes like Web Development, Data Science, Machine learning and also to perform different processes with automation. While working on any field of application we have to work on one common thing called string. So in this article we are going to learn how to remove unwanted characters from string.
Replacement
In this method we simply specify the unwanted element and then that element is removed and an empty place is present in the string. We can understand it in a more better way through the following example
Example
def unwanted_string_words(data, sentence): #Input is provided for sent in sentence: #Check different characters in string data = data.replace(sent, '') #Remove unwanted characters from string return data # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
Output
The output of the above code will be as follows:
Hi My Name Is John
re module
In this method we will use the regular expression module to remove the characters from the string. We can understand its use from the following example:
Example
import re # Do not forget to import the re module or else error might occur def unwanted_string_words(data, sentence): # The input is provided pattern = "[" + re.escape("".join(sentence)) + "]" #We will join all the characters using re.escape return re.sub(pattern, "", data) # We will then use re.sub to remove the unwanted characters # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
Output
The output of the above example will be as follows:
Hi My Name Is John
List Comprehension
By this method we will create a new list only of the wanted characters after removing all the unwanted characters from the original string. We can understand it clearly through the following example:
Example
def unwanted_string_words(data, sentence): #The input is provided return ''.join([sent for sent in data if sent not in sentence]) # All the characters in the string are checked and the characters which are not to be removed sre moved to a new list and some characters are removed. # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
Output
The output of the above provided example will be as follows:
Hi My Name Is John
Translate Method
This is a very useful method to remove characters from a list of dictionaries. The characters to be removed are specified in the translation table and they are removed accordingly. The example of removing characters with the help of translate method is as follows:
Example
def unwanted_string_words(data, sentence): #The input of string is provided translation_table_characters = str.maketrans('', '', ''.join(sentence)) # With the help pf str.maketrans a translation table is made and the characters to be removed are specified return data.translate(translation_table_characters) #The str.translate is used to remove the characters and provide an empty space # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
Output
The output of the above mentioned example will be as follows:
Hi My Name Is John
Filter Function
As the name suggests, in this method we will specify some filter to remove some characters from our string. This is one of the simplest methods of removing characters from a string. We can understand it in a more clear way through the following example:
Example
def unwanted_string_words(data, sentence): # The input of string is given unwanted_string_words = filter(lambda sent: sent not in sentence, data) #Filter will check all the characters of the string # Lambda will be used to look for the unwanted characters in the whole string return ''.join(unwanted_string_words) #After removing the unwanted characters, the remaining characters are moved to a new list # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
Output
The output of the above example will be as follows:
Hi My Name Is John
List & Joining
In this method, whole of the string is created into characters and then all the characters are checked and all the unwanted characters are removed from the list. We can understand it in a more better way through the following example:
Example
def unwanted_string_words(data, sentence): # The input of string is provided unwanted_string_words = [sent for sent in data if sent not in sentence] # List comprehension will be used to check each characters and the unwanted ones will be removed return ''.join(unwanted_string_words) #The remaining characters will be joined to form a string # Example whole_string = "Hi! My Name Is, John" to_be_removed = "!," final_string =unwanted_string_words(whole_string, to_be_removed) print(final_string)
Output
The output of the above example provided will be as follows:
Hi My Name Is John
Conclusion
The editing of the strings present in the program is common process followed by a programmer. So, it is very necessary to know about all the above mentioned methods to be an efficient and fast programmer. One can refer this article to learn about many different methods to remove characters from string.