1. 程式人生 > >python多執行緒爬蟲學習--去除字串中間空格

python多執行緒爬蟲學習--去除字串中間空格

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