python3去掉空格 python如何去掉空格

2021-10-13 15:10:53 字數 839 閱讀 7211

詳細內容

a' ddd dfe dfd efre ddd '

1.strip():把頭和尾的空格去掉a.strip()

'ddd dfe dfd efre ddd'

2.lstrip():把左邊的空格去掉in[5]: a.lstrip()

out[5]: 'ddd dfe dfd efre ddd '

3.rstrip():把右邊的空格去掉

out[6]: ' ddd dfe dfd efre ddd'

4.replace('c1','c2'):把字串裡的c1替換成c2。故可以用replace(' ','')來去掉字串裡的所有空格in[7]: a.replace(' ','')

out[7]: 'ddddfedfdefreddd'

5.split():通過指定分隔符對字串進行切片,如果引數num 有指定值,則僅分隔 num 個子字串in[8]: a.split()

out[8]: ['ddd', 'dfe', 'dfd', 'efre', 'ddd']in[3]: a = 'dfdfd*dfjdf**fdjfd*22*'

in[4]: a

out[4]: 'dfdfd*dfjdf**fdjfd*22*'

in[5]: a.split('*')

out[5]: ['dfdfd', 'dfjdf', '', 'fdjfd', '22', '']

in[6]: a.split('*',2)

out[6]: ['dfdfd', 'dfjdf', '*fdjfd*22*']

6.使用正規表示式》 re.split(r'\s+', 'a b c')

['a', 'b', 'c']

python3 字串去掉空格

先寫乙個table t str.maketrans string.ascii letters,string.ascii letters,c asfd c asd sd c.translate t asfdcasdsd t str.maketrans string.ascii letters,stri...

strip去掉首尾空格和指定字元Python

coding utf 8 建立乙個字串hello world hello world the world is big 利用strip 方法處理hello world字串 blank hello world hello world.strip char hello world hello world...

Python3正則去掉HTML標籤

1.引用一段 import re html 目的是通過第一次soup.find按class粗略篩選並通過soup.find all篩選出列表中的a標籤並讀入href和title屬性 但是由於目標鏈結可能有鏈結,而這是我不想要的.請問如何去除?reg re.compile print reg.sub ...