How to remove characters from a string in Python
(Adapted from a from a post I wrote on Quora...)
There are numerous ways to remove characters from a string in Python...but first, we have to understand that strings in Python are immutable. That means they can't be modified directly, e.g., we can't write something like this:
s[0] = 'x'
to change the first character. Instead we'd write:
s = 'x' + s[1:]
which leverages slicing to generate a brand new string consisting of an 'x' followed by all of the characters of the original string, except for the initial character.
Now, to remove characters, such as all the a’s from a string, one can use the .replace() string method:
>>> s = 'A man, a plan, a canal: Panama'
>>> s = s.replace('a', '')
>>> s
'A mn, pln, cnl: Pnm'
(Once again, to be clear, we are not changing the original string. Instead, the .replace() method is generate a new string, consisting of the original string without the a's.)
We could also use a list comprehension with a filter to “explode” the string into a list of chars, remove the a’s, and then compress it back into a string using .join():
>>> chars = [char for char in s if char != 'a']
>>> chars
['A', ' ', 'm', 'n', ',', ' ', ' ', 'p', 'l', 'n', ',', ' ', ' ', 'c', 'n', 'l', ':', ' ', 'P', 'n', 'm']
>>> ''.join(chars)
'A mn, pln, cnl: Pnm'
Normally, that would be written in one line using a generator expression:
>>> s = ''.join(char for char in s if char != 'a')
>>> s
'A mn, pln, cnl: Pnm'
If you’re interested in removing multiple characters at once, say a’s and n’s, .replace() would not be a good strategy, but the above technique would work:
>>> s = 'A man, a plan, a canal: Panama'
>>> s = ''.join(char for char in s if char not in 'an')
>>>
>>> s
'A m, pl, cl: Pm'
For multiple characters, using Python’s regular expression module would also work:
>>> s = 'A man, a plan, a canal: Panama'
>>> import re
>>> re.sub('[an]', '', s)
'A m, pl, cl: Pm'