I\'ve created a onetime function
a = lambda x: x.replace(\'\\n\', \'\')
b = lambda y: y.replace(\'\\t\', \'\').strip()
c = lambda x: b(a(x))
<
Option 1
str.translate
For starters, if you're replacing a lot of characters with the same thing, I'd 100% recommend str.translate
.
>>> from string import whitespace as wsp
>>> '\n\ttext \there\r'.translate(str.maketrans(dict.fromkeys(wsp, '')))
'texthere'
This syntax is valid with python-3.x only. For python-2.x, you will need to import string
and use string.maketrans
to build the mapping instead.
If you want to exclude whitespace chars itself, then
wsp = set(wsp) - {' '}
Option 2
re.sub
The regex equivalent of the above would be using re.sub
.
>>> import re
>>> re.sub(r'\s+', '', '\n\ttext \there\r')
'texthere'
However, performance wise, str.translate
beats this hands down.