s = '''Hello World \t\n\r\tHi There'''# import the module stringimport string# use the method translate to converts.translate({ord(c): None for c in string.whitespace}>>'HelloWorldHiThere'
与regex
s = ''' Hello World\t\n\r\tHi '''print(re.sub(r"\s+", "", s), sep='') # \s matches all white spaces>HelloWorldHi
替换\n,\t,\r
s.replace('\n', '').replace('\t','').replace('\r','')>' Hello World Hi '
与regex
s = '''Hello World \t\n\r\tHi There'''regex = re.compile(r'[\n\r\t]')regex.sub("", s)>'Hello World Hi There'
与加入
s = '''Hello World \t\n\r\tHi There'''' '.join(s.split())>'Hello World Hi There'