python编程怎样去掉空格
python編程怎樣去掉空格
處理字符串時經常要定制化去掉無用的空格,python 中要么用存在的常規方法,或者用正則處理,那么python編程怎樣去掉空格?python去掉空格常用方式具體有哪些呢?今天就一起來了解下吧!
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:調用字符串的替換方法把空格替換成空
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
關于python怎樣去掉空格,就和大家分享到這里了,學習是永無止境的,學習一項技能更是受益終身,所以,只要肯努力學,什么時候開始都不晚,加油!
總結
以上是生活随笔為你收集整理的python编程怎样去掉空格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android TextView 竖向显
- 下一篇: Ansible Tower 全方位整理