
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to remove a list of characters in string in Python?
In this article, we are going to find out how to remove a list of characters in string in Python.
The first approach is by using the replace() method. This method takes 2 parameters, the character we would like to replace and the character with which we are replacing. This method takes a string as input and we will get the modified string as output.
The replace string method creates a new string by replacing some of the characters in the given string with new ones. The initial string is unaffected and unchanged.
Example
In the example given below, we are taking a string as input and we are removing list of unwanted characters using the replace() method −
str1 = "Welcome to tutorialspoint" print("The given string is") print(str1) print("Removing the character 't' from the input string") print(str1.replace('t',''))
Output
The output of the above example is as follows −
The given string is Welcome to tutorialspoint Removing the character 't' from the input string Welcome o uorialspoin
Using Regular Expressions
The second method involves the use of regular expressions. The technique re.sub is used in conjunction with a regular expression. We use re.sub() to delete the unnecessary characters and replace them with empty spaces.
Example
In the example given below, we are taking a string as input and we are removing a list of characters using Regular expressions
import re str1 = "Welcome to tutorialspoint" print("The given string is") print(str1) print("The updated string is") print(re.sub("e|t", " ",str1))
Output
The output of the above example is as follows −
The given string is Welcome to tutorialspoint The updated string is W lcom o u orialspoin
Using join() and generator
The third technique is to use a generator's join() function. We create a list of unneeded characters, then iterate through the string to see if the character is in the list of undesirable characters. If it isn't, we use the join() function to add that specific character.
Example
In the example given below, we are taking a string as input and we are removing a list of characters using the join() method −
str1 = "Welcome to tutorialspoint" print("The given string is") print(str1) remove = ['e','t'] str1 = ''.join(x for x in str1 if not x in remove) print("The updated string is") print(str1)
Output
The output of the above example is given below −
The given string is Welcome to tutorialspoint The updated string is Wlcom o uorialspoin
- Related Articles
- How to remove specific characters from a string in Python?
- How to remove characters except digits from string in Python?
- How to Remove Characters from a String in Arduino?
- Program to remove duplicate characters from a given string in Python
- Python Program to Remove the Characters of Odd Index Values in a String
- How can we convert a list of characters into a string in Python?
- How to remove certain characters from a string in C++?
- How to remove all special characters, punctuation and spaces from a string in Python?
- How to convert a list of characters into a string in C#?
- Program to remove string characters which have occurred before in Python
- Convert a String to a List of Characters in Java
- Convert list of strings and characters to list of characters in Python
- Python program to extract characters in given range from a string list
- How to remove all non-alphanumeric characters from a string in MySQL?
- Convert List of Characters to String in Java
