- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove specific characters from a string in Python?
The string class has a method replace that can be used to replace substrings in a string. We can use this method to replace characters we want to remove with an empty string. For example:
>>> "Hello people".replace("e", "") "Hllo popl"
If you want to remove multiple characters from a string in a single line, it's better to use regular expressions. You can separate multiple characters by "|" and use the re.sub(chars_to_replace, string_to_replace_with, str). For example:
import re print (re.sub("e|l", " ", "Hello people"))
H o p op
Note: You can also use the [ ] to create group of characters to replace in regex.
- How to Remove Characters from a String in Arduino?
- How to remove characters except digits from string in Python?
- How to remove certain characters from a string in C++?
- Program to remove duplicate characters from a given string in Python
- How to scan a string for specific characters in Python?
- How to remove all special characters, punctuation and spaces from a string in Python?
- How to remove a list of characters in string in Python?
- How to remove all non-alphanumeric characters from a string in MySQL?
- C# Program to remove duplicate characters from String
- How to Remove Punctuations From a String in Python?
- PHP program to remove non-alphanumeric characters from string
- Remove Vowels from a String in Python