Remove a List of Characters from a String in Python

In this article, we will discuss about some techniques, using which we can remove a list of characters from a string in Python Programming Language.

Table Of Contents

Introduction

Suppose we have a string and some specific characters in a list,

example_string = 'There is a Red Bus in the Bus Stand.There are many red Buses in the Bus Stand'
chars_list = ['a','e','i','o','u']

The job is to remove those specific characters from the string using Python Programming Langauge. After removal of these characters, final string should be like,

Thr s  Rd Bs n th Bs Stnd.Thr r mny rd Bss n th Bs Stnd

Let’s see how to do this.

Method 1 : Using join() method

First method that we will be using to remove a list of characters from a string, is the join() method of class string. The join() methods takes an iterable as an input and joins it in a string. It uses calling string object as the separator.

First of all, we will iterate over all characters of string example_string using the list comprehension and at the same time we will select only those characters which are in our list chars_list. So, list comprehension will return a new list of select characters only. Then we will join those characters using join() method, to create a new string. Then we will assign this new string to old variable example_string. It will give an effect that we have all characters in list from the string. Let’s see complete example,

CODE :

# initialized a list with chars which we want to remove.
chars_list = ['a','e','i','o','u']

# initialized an example string.
example_string = 'There is a Red Bus in the Bus Stand.There are many red Buses in the Bus Stand'

# using join method we removed the characters of char_list from example_string.
example_string = ''.join([i for i in example_string if i not in chars_list])

print(example_string)

OUTPUT :

Thr s  Rd Bs n th Bs Stnd.Thr r mny rd Bss n th Bs Stnd

In the above example, we have successfully removed characters in list char_list from the string example_string.

Method 2 : Using re module

In this method, we will use thesub() and escape() method of re module to remove a list of characters from a string. The re stands for Regular Expression, which comes bundled with Python Programming language and mainly used for pattern matching. First method that we will be using here is the sub() method, to remove a list of characters in string. This method needs a regex pattern and a replacement string as arguments. Then it looks for all substrings in string that matches the given pattern and replaces them with the given substring. To create regex pattern that matches all the characters in list, we will be using join() and escape() methods.

  • sub() method : Stands for substitution and returns a string with replaced value base on the regex pattern and replacement substring.This method recieves five parameters :
    • pattern : A regex pattern which needs to be matched. It can also be pattern object.
    • repl : The replacemet of the pattern found.
    • string : The input string.
    • count : Specifies the maximum or minimum number of repalcement. If 0, then completely skips. If not provided, then replaces every instance. It is Optional.
    • flags : This modifies the standard behaviour. It is Optional.

SYNTAX:

re.sub(pattern, repl, string, count, flags)
  • escape() : The escape() method helps to build a pattern based on some strings by escaping some special characters.

Now See the example code below,

import re 

# initialized a list with chars which we want to remove.
chars_list = ['a','e','i','o','u']

# initialized an example string.
example_string = 'There is a Red Bus in the Bus Stand.There are many red Buses in the Bus Stand'

# creating a pattern based on chars_list using join() and re.escape()
pattern = '[' + re.escape(''.join(chars_list)) + ']'

# storing the new string with removed characters.
example_string = re.sub(pattern, '', example_string )

print(example_string)

OUTPUT :

Thr s  Rd Bs n th Bs Stnd.Thr r mny rd Bss n th Bs Stnd

So in the above example, by using the sub(), escape() and join() methods, we have successfully removed chars in list chars_list from the string example_string.

Method 3 : Using translate()

Another technique that we can use to remove a list of characters from a string is by using the translate() method of class string. This method replaces the characters in string based on a mapping table. It requires only one parameter which is the mapped table or dictionary based on which replacement will be performed. Let’s see the example,

CODE :

# initialized a list with chars which we want to remove.
chars_list = ['a','e','i','o','u']

# initialized an example string.
example_string = 'There is a Red Bus in the Bus Stand.There are many red Buses in the Bus Stand'

# CREATING A NEW_STIRNG BY REMOVING CHARS FROM CHARS_LIST.
example_string = example_string.translate(dict.fromkeys(map(ord, chars_list)))

print(example_string)

OUTPUT :

<class 'list'>
<class 'str'>
Thr s  Rd Bs n th Bs Stnd.Thr r mny rd Bss n th Bs Stnd

In above example, by using the str.translate() method we have successfully removed characters in list chars_list from the string example_string. In this method we have used map() method and ord() method to create a mapping table a dictionary. In this dictionary, the ascii value of the characters that needs to be deleted is mapped with None. Now, based on this mapping translate() function deleted those characters from the string.

Summary

So in this method we have used three different method to remove a list of characters from a string. All the methods are very basic and super easy to understand. You can always use any of the methods above, but the method with shorter and easy syntax is method one, in which basic str.join() method along with for loop has been used.

Also we have used Python 3.9.12 for writing example codes. Type python –version to check your python version. Always try to write example codes on your machine. Thanks.

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