python编程怎样去掉空格
python編程怎樣去掉空格
處理字符串時(shí)經(jīng)常要定制化去掉無(wú)用的空格,python 中要么用存在的常規(guī)方法,或者用正則處理,那么python編程怎樣去掉空格?python去掉空格常用方式具體有哪些呢?今天就一起來(lái)了解下吧!
1、去掉右邊空格:
string = " * it is blank space test * "
print (string.rstrip())
result:
- it is blank space test *
2、去掉左邊空格:
string = " * it is blank space test * "
print (string.lstrip())
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怎樣去掉空格,就和大家分享到這里了,學(xué)習(xí)是永無(wú)止境的,學(xué)習(xí)一項(xiàng)技能更是受益終身,所以,只要肯努力學(xué),什么時(shí)候開(kāi)始都不晚,加油!
總結(jié)
以上是生活随笔為你收集整理的python编程怎样去掉空格的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android TextView 竖向显
- 下一篇: python字符串剔除空格和逗号_用逗号