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

(编辑:jimmy 日期: 2024/9/30 浏览:2)

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清除字符串中间空格的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。