美文网首页
python去除字符串中间空格的方法

python去除字符串中间空格的方法

作者: ouchaochao | 来源:发表于2018-12-12 14:58 被阅读0次

1、使用字符串函数replace

>>> a = 'hello world'
>>> a.replace(' ', '')
helloworld

2、使用字符串函数split

>>> a = ''.join(a.split())
>>> print(a)
helloworld

3、使用正则表达式

>>> import re
>>> str = re.compile()
>>> str = re.compile(' ')
>>> b = str.sub('', a)
>>> print(b)
helloworld

相关文章

网友评论

      本文标题:python去除字符串中间空格的方法

      本文链接:https://www.haomeiwen.com/subject/qmtphqtx.html