码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-10-28 17:46:22      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:python   string   inf   number   函数   pil   print   key   str   

1、使用字符串函数replace

>>> a = ‘hello world‘

>>> a.replace(‘ ‘, ‘‘)
‘helloworld‘
  • 1
  • 2
  • 3
  • 4

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

2、使用字符串函数split

>>> a = ‘‘.join(a.split())

>>> print(a)
helloworld
  • 1
  • 2
  • 3
  • 4

3、使用正则表达式

>>> import re
>>> strinfo = re.compile()

>>> strinfo = re.compile(‘ ‘)

>>> b = strinfo.sub(‘‘, a)
>>> print(b)
helloworld

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

标签:python   string   inf   number   函数   pil   print   key   str   

原文地址:http://www.cnblogs.com/defifind/p/7747171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!