Python: Remove characters from string by regex and 4 other ways

Removing characters from string by regex and many other ways in Python.

In this article we will get to know about deleting characters from string in python by regex() and various other functions.

Removing characters from string by regex :

sub() function of regex module in Python helps to get a new string by replacing a particular pattern in the string by a string replacement. If no pattern found, then same string will be returned.

Removing all occurrences of a character from string using regex :

Let we want to delete all occurrence of ‘a’ from a string. We can implement this by passing in sub() where it will replace all ‘s’ with empty string.

#Program :

import re
origin_string = "India is my country"

pattern = r'a'

# If found, all 'a' will be replaced by empty string
modif_string = re.sub(pattern, '', origin_string )
print(modif_string)
Output :
Indi is my country

Removing multiple characters from string using regex in python :

Let we want to delete all occurrence of ‘a’, ‘i’. So we have to replace these characters by empty string.

#Program :

import re
origin_string = "India is my country"
pattern = r'[ai]'
# If found, all 'a' & 'i' will be replaced by empty string
modif_string = re.sub(pattern, '', origin_string )
print(modif_string) 
Output :
Ind s my country

Removing characters in list from the string in python :

Let we want to delete all occurrence of ‘a’ & ‘i’ from a string and also these characters are in a list.

#Program :

import re
origin_string = "India is my country"
char_list = ['i','a']
pattern = '[' + ''.join(char_list) + ']'
# All charcaters are removed if matched by pattern
modif_string = re.sub(pattern, '', origin_string)
print(modif_string)
Output :
Ind s my country

Removing characters from string using translate() :

str class of python provide a function translate(table). The characters in the string will be replaced on the basis of mapping provided in translation table.

Removing all occurrence of a character from the string using translate() :

Let we want to delete all occurrence of ‘i’ in a string. For this we have to pass a translation table to translate() function where ‘i’ will be mapped to None.

#Program :

origin_string = "India is my country"
# If found, remove all occurence of 'i' from string
modif_string = origin_string.translate({ord('i'): None})
print(modif_string)
Output :
Inda s my country

Removing multiple characters from the string using translate() :

Let, we want to delete ‘i’, ‘y’ from the above string.

#Program :

org_string= "India is my country"
list_char = ['y', 'i']
# Remove all 's', 'a' & 'i' from the string
mod_string = org_string.translate( {ord(elem): None for elem in list_char} )
print(mod_string)
Output :
Inda s m countr

Removing characters from string using replace()  :

Python provides str class, from which the replace() returns the copy of a string by replacing all occurrence of substring by a replacement. In the above string we will try to replace all ‘i’ with ‘a’.

#Program :

origin_string = "India is my country"
# Replacing all of 's' with 'a'
modif_string = origin_string.replace('i', 'a')
print(modif_string)
Output :
Indaa as my country

Removing characters from string using join() and generator expression :

Let we have a list with some characters. For removing all characters from string that are in the list, we would iterate over each characters in the string and join them except those which are not in the list.

#Program :

origin_string= "India is my country"
list_char = ['i', 'a', 's']
# Removes all occurence of 'i', 'a','s' from the string
modif_string = ''.join((elem for elem in origin_string if elem not in list_char))
print(modif_string)
Output :
Ind  my country

Removing characters from string using join and filter() :

filter() function filter the characters from string based on logic provided in call back function. If we provide a call back function as lambda function, it checks if the characters in the list are filtered are not. After that it joins the remaining characters to from a new string i.e. it eliminates all occurrence of characters that are in the list form a string.

#Programn :

origin_string = "India is my country"
list_char = ['i', 'a', 's']
# To remove all characters in list, from the string
modif_string = ''.join(filter(lambda k: k not in list_char, origin_string))
print(modif_string)
Output :
Ind  my country