三種方法如下:
創(chuàng)新互聯(lián)2013年開(kāi)創(chuàng)至今,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元鼓樓做網(wǎng)站,已為上家服務(wù),為鼓樓各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
用replace函數(shù):
your_str.replace('?',?'')
a?=?'hello?word'??#?把a(bǔ)字符串里的word替換為python
a.replace('word','python')??#?輸出的結(jié)果是hello?python
用split斷開(kāi)再合上:
''.join(your_str.split())
用正則表達(dá)式來(lái)完成替換:
import?re?strinfo?=?re.compile('word')
b?=?strinfo.sub('python',a)?
print?b?
#?結(jié)果:hello?python
1.去掉左邊空格
string = " * it is blank space test * "
print (string.lstrip())
result:
* it is blank space test *
2.去掉右邊空格
string = " * it is blank space test * "
print (string.rstrip())
result:
* it is blank space test *
3.去掉左右兩邊空格
string = " * it is blank space test * "
print (string.strip())
result:
* it is blank space test *
4.去掉所有空格
有兩種方式
eg1:調(diào)用字符串的替換方法把空格替換成空
string = " * it is blank space test * "
str_new = string.replace(" ", "")
print str_new
result:
*itisblankspacetest*
eg2:正則匹配把空格替換成空
import re
string = " * it is blank space test * "
str_new = re.sub(r"\s+", "", string)
print str_new
result:
*itisblankspacetest*
關(guān)于python去掉空格常用方式有哪些,環(huán)球青藤小編就和大家分享到這里了,學(xué)習(xí)是永無(wú)止境的,學(xué)習(xí)一項(xiàng)技能更是受益終身,所以,只要肯努力學(xué),什么時(shí)候開(kāi)始都不晚。如果您還想繼續(xù)了解關(guān)于python編程的學(xué)習(xí)方法及素材等內(nèi)容,可以點(diǎn)擊本站其他文章學(xué)習(xí)。
'''
在Python中字符串處理函數(shù)里有三個(gè)去空格的函數(shù):
strip?同時(shí)去掉左右兩邊的空格
lstrip?去掉左邊的空格
rstrip?去掉右邊的空格
'''
#具體示例如下:
a="?gho??stwwl?"
print(a.lstrip())
print(a.rstrip())
print(a.strip())
#去掉中間多余的空格
s=''
for?i?in?range(len(a)):
if?a[i]=='?'?and?ilen(a)-1?and?a[i+1]=='?':
continue
s+=a[i]
print(s)#配合strip()使用,全部多余空格去掉
字符串,rm為要?jiǎng)h除的字符序列
str.strip(rm) : 刪除s字符串中開(kāi)頭、結(jié)尾處,位于 rm刪除序列的字符
str.lstrip(rm) : 刪除s字符串中開(kāi)頭(左邊)處,位于 rm刪除序列的字符
str.rstrip(rm) : 刪除s字符串中結(jié)尾(右邊)處,位于 rm刪除序列的字符
str.replace(‘s1’,’s2’) : 把字符串里的s1替換成s2。故可以用replace(’ ‘,”)來(lái)去掉字符串里的所有空格
str.split() : 通過(guò)指定分隔符對(duì)字符串進(jìn)行切分,切分為列表的形式。
去除兩邊空格:
str = ' hello world '
str.strip()
'hello world'
1
2
3
1
2
3
去除開(kāi)頭空格:
str.lstrip()
'hello world '
1
2
1
2
去除結(jié)尾空格:
str.rstrip()
' hello world'
1
2
1
2
去除全部空格:
str.replace(' ','')
'helloworld'
1
2
1
2
將字符串以空格分開(kāi):
str.split()
['hello', 'world']
文章標(biāo)題:去空格函數(shù)python 去空格函數(shù)oracle
網(wǎng)頁(yè)網(wǎng)址:http://chinadenli.net/article6/hgsiog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、域名注冊(cè)、網(wǎng)站設(shè)計(jì)、Google、品牌網(wǎng)站制作、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)