Remove specific characters from a Python string
This post will discuss how to remove specific characters from a string in Python.
Since strings are immutable in Python, you cannot simply remove characters from it. However, you can create a copy of a string without specific characters. This post provides an overview of several functions to accomplish this.
1. Using str.translate() function
An efficient solution is to use the str.translate() function to remove certain characters from a string. It maps each character of the string through a translation table, which can be created using the str.maketrans() function.
|
1 2 3 4 5 6 7 8 |
if __name__ == '__main__': s = 'Hello, World!!' chars = '.,!' res = s.translate(str.maketrans('', '', chars)) print(res) # Hello World |
If you have a list of characters, you can use the following code:
|
1 2 3 4 5 6 7 8 |
if __name__ == '__main__': s = 'Hello, World!!' chars = ['.', ',', '!'] res = s.translate(str.maketrans('', '', ''.join(chars))) print(res) # Hello World |
You can also specify a dictionary to str.maketrans() function. Following is a simple example demonstrating the mapping variant of str.translate():
|
1 2 3 4 5 6 7 8 |
if __name__ == '__main__': s = 'Hello, World!!' chars = ['.', ',', '!'] res = s.translate(str.maketrans({ord(x): '' for x in chars})) print(res) # Hello World |
2. Using Set
Another option is to filter the string to remove characters that match with the given list of characters.
|
1 2 3 4 5 6 7 8 9 10 |
if __name__ == '__main__': s = 'Hello, World!!' removed_chars = ['.', ',', '!'] chars = set(removed_chars) res = ''.join(filter(lambda x: x not in chars, s)) print(res) # Hello World |
Here’s a generator version of the above code:
|
1 2 3 4 5 6 7 8 9 10 |
if __name__ == '__main__': s = 'Hello, World!!' removed_chars = ['.', ',', '!'] chars = set(removed_chars) res = ''.join(c for c in s if c not in chars) print(res) # Hello World |
3. Using Regex
Another plausible way is to use regular expressions for removing certain characters from a string. This can be done using the re.sub() function. All characters enclosed within the square brackets constitute a character class, which will be replaced with the second parameter to re.sub(), i.e., an empty string.
|
1 2 3 4 5 6 7 8 9 |
import re if __name__ == '__main__': s = 'Hello, World!!' res = re.sub('[.,!]', '', s) print(res) # Hello World |
If you want to remove all characters present in a literal string, you can use it below. It uses the re.escape() function to escape characters having a special meaning in a regular expression.
|
1 2 3 4 5 6 7 8 9 10 11 |
import re if __name__ == '__main__': s = 'Hello, World!!' removed_chars = '.,!' chars = '[%s]+' % re.escape(removed_chars) res = re.sub(chars, '', s) print(res) # Hello World |
If you have a list of characters that need to be removed from the string, you can do like:
|
1 2 3 4 5 6 7 8 9 10 11 |
import re if __name__ == '__main__': s = 'Hello, World!!' removed_chars = '.', ',', '!' chars = '|'.join(map(re.escape, removed_chars)) res = re.sub(chars, '', s) print(res) # Hello World |
That’s all about removing specific characters from a string in Python.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)