夕阳渐渐迫近地平线,霞光从地平线晕染开来,将天边的云朵渲染得一片通红。山岗上,我静静地望着那轮夕阳。霞光将我的身影剪裁的冗长。暖暖的阳光将我投射成透明,仿佛随时都会蒸发。
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清除字符串中间空格的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。