Python strip lstrip rstrip使用方法

1.簡介

   Python中的strip用於去除字符串的首尾字符,同理,lstrip用於去除左邊的字符,rstrip用於去除右邊的字符。這三個函數均可傳入一個參數,指定要去除的首尾字符。須要注意的是,傳入的是一個字符數組,編譯器去除兩端全部相應的字符,直到沒有匹配的字符,好比:python

#輸入
theString = 'saaaay yes no yaaaass'
print theString.strip('say')
#輸出
yes no

2.舉例說明

#輸入
theString = 'saaaay yes no yaaaass'
print theString.strip('say') 
print theString.strip('say ') #say後面有空格 
print theString.lstrip('say') 
print theString.rstrip('say')
#輸出
yes no 
es no 
yes no yaaaass 
saaaay yes no

3.注意事項數組

#當沒有參數時功能爲去除收尾空格
str= " aaa"
print str.strip()
相關文章
相關標籤/搜索