三種方法如下:
創(chuàng)新互聯(lián)2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元鼓樓做網(wǎng)站,已為上家服務(wù),為鼓樓各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792
用replace函數(shù):
your_str.replace('?',?'')
a?=?'hello?word'??#?把a字符串里的word替換為python
a.replace('word','python')??#?輸出的結(jié)果是hello?python
用split斷開再合上:
''.join(your_str.split())
用正則表達式來完成替換:
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í)是永無止境的,學(xué)習(xí)一項技能更是受益終身,所以,只要肯努力學(xué),什么時候開始都不晚。如果您還想繼續(xù)了解關(guān)于python編程的學(xué)習(xí)方法及素材等內(nèi)容,可以點擊本站其他文章學(xué)習(xí)。
'''
在Python中字符串處理函數(shù)里有三個去空格的函數(shù):
strip?同時去掉左右兩邊的空格
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為要刪除的字符序列
str.strip(rm) : 刪除s字符串中開頭、結(jié)尾處,位于 rm刪除序列的字符
str.lstrip(rm) : 刪除s字符串中開頭(左邊)處,位于 rm刪除序列的字符
str.rstrip(rm) : 刪除s字符串中結(jié)尾(右邊)處,位于 rm刪除序列的字符
str.replace(‘s1’,’s2’) : 把字符串里的s1替換成s2。故可以用replace(’ ‘,”)來去掉字符串里的所有空格
str.split() : 通過指定分隔符對字符串進行切分,切分為列表的形式。
去除兩邊空格:
str = ' hello world '
str.strip()
'hello world'
1
2
3
1
2
3
去除開頭空格:
str.lstrip()
'hello world '
1
2
1
2
去除結(jié)尾空格:
str.rstrip()
' hello world'
1
2
1
2
去除全部空格:
str.replace(' ','')
'helloworld'
1
2
1
2
將字符串以空格分開:
str.split()
['hello', 'world']
文章標(biāo)題:去空格函數(shù)python 去空格函數(shù)oracle
網(wǎng)頁網(wǎng)址:http://muchs.cn/article6/hgsiog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、域名注冊、網(wǎng)站設(shè)計、Google、品牌網(wǎng)站制作、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)