python中有没有函数可以一次性去除空格,换行符,制表符

如题所述

正则表达式有一个sub函数可以实现批量替换字符串

# coding=utf-8
# using py27
import re
string = '123 \t456\n 789'  # 待替换的字符串
pattern = re.compile('\t|\n| ')
replaced_string = re.sub(pattern, '', string)
print(replaced_string)

输出结果:

123456789

温馨提示:答案为网友推荐,仅供参考
相似回答