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()
相关文章
相关标签/搜索