欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python清除字符串中间空格的实例讲解

程序员文章站 2022-05-20 18:14:23
1、使用字符串函数replace >>> a = 'hello world' >>> a.replace(' ', '')...

1、使用字符串函数replace

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

看上这种方法真的是很笨。

2、使用字符串函数split

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

3、使用正则表达式

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

以上这篇python清除字符串中间空格的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。