网络编程
位置:首页>> 网络编程>> Python编程>> python清除字符串中间空格的实例讲解

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

作者:yesNow_xiao  发布时间:2023-12-29 22:40:24 

标签:python,字符串,空格

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

来源:https://blog.csdn.net/depers15/article/details/52223956

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com