python多线程爬虫学习--去除字符串中间空格

来源:互联网 发布:mysql数据库常用语句 编辑:程序博客网 时间:2024/05/16 00:28

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

1、使用字符串函数replace

>>> a = 'hello world'>>> a.replace(' ', '')'helloworld'
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

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

2、使用字符串函数split

>>> a = ''.join(a.split())>>> print(a)helloworld
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

3、使用正则表达式

>>> import re>>> strinfo = re.compile()>>> strinfo = re.compile(' ')>>> b = strinfo.sub('', a)>>> print(b)helloworld
阅读全文
0 0