I think the fastest way is to use str.translate() :
import string s = "a\nb\rc\td" print s.translate(string.maketrans("\n\t\r", " "))
prints
abcd
EDIT : Since this has again turned into a discussion about performance, here are some numbers. For long strings, translate() is faster than using regular expressions:
s = "a\nb\rc\td " * 1250000 regex = re.compile(r'[\n\r\t]') %timeit t = regex.sub(" ", s)
Which is about 40 times.
Sven marnach
source share