Python 一次性祛除字符串中的所有空格

  • 方法一
new_str = "".join(str.split()) 

方法二
使用正则

import re
params = re.compile("\s+") #匹配任意空白字符,等价于 [\t\n\r\f].
new_string = re.sub(params,'',str)
print(new_string)

发布了127 篇原创文章 · 获赞 25 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_44224529/article/details/103828359