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

In this article we will discuss different ways to delete single or multiple characters from string in python either by using regex() or translate() or replace() or join() or filter().

Remove characters from string using regex

Python’s regex module provides a function sub() i.e.

re.sub(pattern, repl, string, count=0, flags=0)

It returns a new string. This new string is obtained by replacing all the occurrences of the given pattern in the string by a replacement string repl. If the pattern is not found in the string, then it returns the same string.

Let’s use this to remove characters from a string,

Remove all occurrences of a character from string using regex

Suppose we want to delete all the occurrences of character ‘s’ from the string. For that we need to pass such a pattern in the sub() function, that matches all the occurrences of character ‘s’ in the given string. Then sub() function should replace all those characters by an empty string i.e.

import re

org_string = "This is a sample string"

pattern = r's'
# Replace all occurrences of character s with an empty string
mod_string = re.sub(pattern, '', org_string )

print(mod_string)

Output

Thi i a ample tring

It removed all the occurrences of character ‘s’ from the string.

Remove multiple characters from string using regex in python

Suppose we want to delete all the occurrences of character ‘s’, ‘a’ and ‘i’ from the string. For that we need to pass such a pattern in the sub() function, that matches all the occurrences of character ‘s’, ‘a’ & ‘i’ in the given string. Then sub() function should replace all those characters by an empty string i.e.

import re

org_string = "This is a sample string"

pattern = r'[sai]'

# Remove characters 's', 'a' and 'i' from a string
mod_string = re.sub(pattern, '', org_string)

print(mod_string)

Output

Th   mple trng

It removed all the occurrences of character ‘s’, ‘a’ and ‘i’ from the string.

Remove characters in list from the string in python.

Suppose we want to delete all the occurrences of character ‘s’, ‘a’ and ‘i’ from the string and all these characters are in a list i.e.

list_of_char = ['s', 'a', 'i']

In this case we will create our pattern by joining all characters in the string and the use sub() function to delete these characters from the string,

import re

list_of_char = ['s', 'a', 'i']

pattern = '[' + ''.join(list_of_char) + ']'
# Remove characters matched by pattern
mod_string = re.sub(pattern, '', org_string)

print(mod_string)

Output:

Th   mple trng

It removed all the occurrences of character ‘s’, ‘a’ and ‘i’ from the string.

Remove characters from string using translate()

In python, str class provides a function translate(table). It replaces the characters in string based on the mapping provided in the translation table.  Let’s use this to remove single or multiple characters from string,

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

Suppose we want to delete all occurrences of character ‘s’ from the string. For that we will pass a translation table to the translate() function. In the translation table, character ‘s’ will be mapped to None i.e.

org_string = "This is a sample string"

# Remove all occurrence of a character 's' from the string
mod_string = org_string.translate({ord('s'): None})

print(mod_string)

Output:

Thi i a ample tring

It will replaced all the occurrences of character ‘s’ with None in the string i.e. it removed all occurrences of character ‘s’ from the string.

Remove multiple characters from the string using translate()

Suppose we want to delete all occurrences of character ‘s’, ‘a’ & ‘i’ from the string. For that we will pass a translation table to the translate() function, where characters ‘s’, ‘a’ & ‘i’ will be mapped to None i.e.

list_of_char = ['s', 'a', 'i']

# Remove all occurrence of a characters 's', 'a' & 'i' from the string
mod_string = org_string.translate( {ord(elem): None for elem in list_of_char} )

print(mod_string)

Output:

Th   mple trng

It will remove all occurrences of characters ‘s’, ‘a’ & ‘i’ from the string.

Remove characters from string using replace()

In Python, str class provides a function replace() i.e.

replace(sub_string, repl)

It returns a copy of string by replacing all occurrences of sub_string with repl.

Let’s use to remove all occurrence of a character ‘s’ from the string,

org_string = "This is a sample string"

# Remove all occurrence of a character 's' from the string
mod_string = org_string.replace('s', '')

print(mod_string)

Output:

Thi i a ample tring

Remove characters from string using join() and generator expression

Suppose we have a list of characters i.e.

list_of_char = ['s', 'a', 'i']

Now to remove all occurrences of these characters from the string. We can iterate over each character in the string and join them back except the characters which are in list i.e.

list_of_char = ['s', 'a', 'i']

# Remove all characters in list, from the string
mod_string = ''.join((elem for elem in org_string if elem not in list_of_char))

print(mod_string)

Output:

Th   mple trng

It removed all occurrences of characters ‘s’, ‘a’ & ‘i’ from the string.

Remove characters from string using join and filter()

Instead of filtering characters using a for loop and generator expression, we can filter them using filter() function and then join back all filtered characters i.e.

org_string = "This is a sample string"

list_of_char = ['s', 'a', 'i']

# Remove all characters in list, from the string
mod_string = ''.join(filter(lambda k: k not in list_of_char, org_string))

print(mod_string)

Output:

Th   mple trng

It filtered the characters from the string based on logic provided as call back function. As the call-back function, we provided a lambda function which checked if character is in list of filtered characters or not. Then joined the remaining characters to create a new string. So basically, it removed all occurrences of characters ‘s’, ‘a’ & ‘i’ from the string.

These were the different ways to remove characters from a string.

The complete example is as follows,

import re

def main():

    print('****** Remove characters from string by regex ******')

    print('*** Remove all occurrences of a character from string using regex ***')

    org_string = "This is a sample string"

    pattern = r's'
    # Replace all occurrences of character s with an empty string
    mod_string = re.sub(pattern, '', org_string )

    print(mod_string)

    print('*** Remove multiple characters from string using regex ***')

    org_string = "This is a sample string"

    pattern = r'[sai]'

    # Remove characters 's', 'a' and 'i' from a string
    mod_string = re.sub(pattern, '', org_string)

    print(mod_string)

    print('*** Python remove characters in list from the string ***')

    list_of_char = ['s', 'a', 'i']

    pattern = '[' + ''.join(list_of_char) + ']'
    # Remove characters matched by pattern
    mod_string = re.sub(pattern, '', org_string)

    print(mod_string)

    print('*** Remove characters from string using translate()***')

    org_string = "This is a sample string"

    # Remove all occurrence of a character 's' from the string
    mod_string = org_string.translate({ord('s'): None})

    print(mod_string)

    org_string = "This is a sample string"

    list_of_char = ['s', 'a', 'i']

    # Remove all occurrence of a characters 's', 'a' & 'i' from the string
    mod_string = org_string.translate({
        ord(elem):
        None for elem in list_of_char})

    print(mod_string)

    print('*** Remove a character from string using replace()***')

    org_string = "This is a sample string"

    # Remove all occurrence of a character 's' from the string
    mod_string = org_string.replace('s', '')

    print(mod_string)

    print('*** Remove multiple character from string using join() ***')

    org_string = "This is a sample string"

    list_of_char = ['s', 'a', 'i']

    # Remove all characters in list, from the string
    mod_string = ''.join((elem for elem in org_string if elem not in list_of_char))

    print(mod_string)

    print('****** Remove multiple characters from string using filter() ******')

    org_string = "This is a sample string"

    list_of_char = ['s', 'a', 'i']

    # Remove all characters in list, from the string
    mod_string = ''.join(filter(lambda k: k not in list_of_char, org_string))

    print(mod_string)


if __name__ == '__main__':
    main()

Output:

****** Remove characters from string by regex ******
*** Remove all occurrences of a character from string using regex ***
Thi i a ample tring
*** Remove multiple characters from string using regex ***
Th   mple trng
*** Python remove characters in list from the string ***
Th   mple trng
*** Remove characters from string using translate()***
Thi i a ample tring
Th   mple trng
*** Remove a character from string using replace()***
Thi i a ample tring
*** Remove multiple character from string using join() ***
Th   mple trng
****** Remove multiple characters from string using filter() ******
Th   mple trng

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top