How do i remove multiple letters from a string in python?
In this article, we will discuss four different ways to delete multiple characters from a string in python. Suppose we have a string “A small sample String for testing” and a list of characters that need to be deleted from string i.e. Let’s see how to delete these characters from the
string. The string class in python, provides a function translate(). It accepts a translation table as an argument and replaces the characters in string based on the mapping in the translation table. We can create a translation table, where each character that we want to be deleted from string, will be mapped to an empty string. Like, We will pass this translation table to translate() function as an argumment. Due to which translate() function will replace all the occurrences of these characters with an empty string. Basically it will remove all the occurrences of these characters from the string. For example, Advertisements sample_str = 'A small sample String for testing' # A list containing multiple characters, that needs to be deleted from the string. list_of_chars = ['s', 't', 'a', 'A', ' '] # Create a mapping table to map the characters # to be deleted with empty string translation_table = str.maketrans('', '', ''.join(list_of_chars)) # Remove multiple characters from the string sample_str = sample_str.translate(translation_table) print(sample_str) Output: mllmpleSringforeing It removed all occurrences of multiple characters from the string. Delete multiple characters from string using regexIn Python, the regex module provides a function to replace the contents of a string based on a matching regex pattern. Signature of function is like this, sub(pattern, replacement_str, original_str) We can use this to remove multiple characters from a string. For this we need to pass a regex pattern that matches all the occurrences of the given characters. Also, as a replacement string we need to pass a empty string. For example, let’s see how to delete characters ‘s’, ‘t’, ‘a’, ‘A’ and ‘ ‘ from a string using regex, import re sample_str = 'A small sample String for testing' # A list containing multiple characters, that needs to be deleted from the string. list_of_chars = ['s', 't', 'a', 'A', ' '] # Create regex pattern to match all characters in list pattern = '[' + ''.join(list_of_chars) + ']' # Remove multiple characters from the string sample_str = re.sub(pattern, '', sample_str) print(sample_str) Output: mllmpleSringforeing It removed all occurrences of ‘s’, ‘t’, ‘a’, ‘A’ and ‘ ‘ from the string. Delete multiple characters from string using replace()The string class provides a function to replace a sub-string in a string i.e. str.replace(to_be_replaced, replacement) It accepts two arguments i.e. the string to be replaced and the replacement string. It returns a copy of the calling string object but with the changed contents i.e. after replacing all the occurrences of sub-string to_be_replaced with the given replacement string. So, to delete multiple characters from a string using replace() function, follow this logic: Iterate over all the characters to be deleted and for each character, pass it to the replace() function along with the empty string. For example, sample_str = 'A small sample String for testing' # A list containing multiple characters, that needs to be deleted from the string. list_of_chars = ['s', 't', 'a', 'A', ' '] # Remove multiple characters from the string for character in list_of_chars: sample_str = sample_str.replace(character, '') print(sample_str) Output: mllmpleSringforeing It removed all occurrences of multiple characters i.e. ‘s’, ‘t’, ‘a’, ‘A’ and ‘ ‘ from the string. Delete multiple characters from string using filter() and join()In Python, you can use the filter() function to filter all the occurences of a characters from a string. Steps are as follows,
For example, sample_str = 'A small sample String for testing' # A list containing multiple characters, that needs to be deleted from the string. list_of_chars = ['s', 't', 'a', 'A', ' '] # Filter multiple characters from string filtered_chars = filter(lambda item: item not in list_of_chars, sample_str) # Join remaining characters in the filtered list sample_str = ''.join(filtered_chars) print(sample_str) Output: mllmpleSringforeing Summary: We learned about different ways to delete multiple characters from a string in python. How do I remove multiple characters from a string in Python?Use str.. original_string = "!( Hell@o)". characters_to_remove = "!()@". new_string = original_string.. for character in characters_to_remove:. new_string = new_string. replace(character, ""). print(new_string). How do I remove certain letters from a string in Python?Using translate():
translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "" .
How do I strip multiple characters from a string?To strip multiple characters in Python, use the string strip() method. It removes the whitespace from the beginning and end of the string by default. But this method also takes an argument. You have to pass the character in the method and it will remove it.
|