
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
Remove List of Characters from String in Python
In Python, Strings are the immutable sequences of characters. While working with them, we will come across situations where we need to remove the unwanted characters (like punctuation, special symbols, etc.).
In this article, we are going to find out how to remove a list of characters from a string. Python provides several ways to achieve this using the built-in methods and list comprehensions.
Using Python str.replace() Method
The python str.replace() method is used to replace all occurrences of one substring in a string with another substring.
This method takes 2 parameters: the character (or substring) we would like to replace and the character (or substring) with which we are replacing. This method replaces the existing (specified) characters in the current string with the new characters and returns the result.
Syntax
Following is the syntax for Python str.replace() method -
str.replace(oldvalue, newvalue, count)
Example
Let's look at the following example, where we are going to remove the characters in the list ['a', 'e', 'i'] from the string "TutorialsPoint".
str1 = "TutorialsPoint" x = ['a', 'e', 'i'] for char in x: str1 = str1.replace(char, '') print(str1)
The output of the above program is as follows -
TutorlsPont
Using Python join() Method
The second approach is by using the Python join() method, which takes all the elements in an iterable (such as a list or string) separated by the given separator and joins them into one string.
In this approach, we are going to create a list of unwanted characters, then check each character and include it in the result only if it is not in the unwanted characters list, and the join() method concatenates the filtered characters into a new string.
Syntax
Following is the syntax for the Python join() method -
str.join(iterable)
Example
Consider the following example, where we are going to remove the characters ['e', 'l'] from the string "Welcome".
str1 = "Welcome" x = ['e', 'l'] result = ''.join([char for char in str1 if char not in x]) print(result)
The following is the output of the above program -
Wcom
Using Python str.translate() and str.maketrans() Method
The third approach is by using the maketrans() and translate() methods, Where the Python maketrans() method is used to create the translation table that contains the mapping information of several characters.
Syntax
Following is the syntax for Python str.maketrans() method -
str.maketrans(x[, y[, z]])
And the Python translate() method is the sequel to the maketrans() method in the string module. It uses the translation table generated by the maketrans() method.
Syntax
Following is the syntax for the Python translate() method -
str.translate(table);
In this approach, we are going to use the maketrans() for creating the translation table that maps each character in the third argument to none, and the translate() uses this table and removes all the mapped characters.
Example
In the following example, we are going to remove all the special characters from the string "Tu*@torials#@*Point#2025" using the maketrans() and translate() methods.
str1 = "Tu*@torials#@*Point#2025" x = ['@', '#', '*'] y = str.maketrans('', '', ''.join(x)) result = str1.translate(y) print(result)
The following is the output of the above program -
TutorialsPoint2025