Python strip lstrip rstrip使用方法

yipeiwu_com5年前Python基础

    注意的是,传入的是一个字符数组,编译器去除两端所有相应的字符,直到没有匹配的字符,比如:

theString = 'saaaay yes no yaaaass' 
print theString.strip('say')

theString依次被去除首尾在['s','a','y']数组内的字符,直到字符在不数组内。所以,输出的结果为:
yes no
比较简单吧,lstrip和rstrip原理是一样的。注意:当没有传入参数时,是默认去除首尾空格的。

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

相关文章

让Python更加充分的使用Sqlite3

我最近在涉及大量数据处理的项目中频繁使用 sqlite3。我最初的尝试根本不涉及任何数据库,所有的数据都将保存在内存中,包括字典查找、迭代和条件等查询。这很好,但可以放入内存的只有那么多...

Python3几个常见问题的处理方法

1. 编码问题: 遇到了几个字符串转换问题,总结如下: # str to bytes str.encode(s) # bytes to str bytes.decode(b)...

python 输出上个月的月末日期实例

如下所示: import dateutil def before_month_lastday(ti): today=dateutil.parser.parse(str(ti))...

利用Pandas 创建空的DataFrame方法

平时写pyhton的时候习惯初始化一些list啊,tuple啊,dict啊这样的。一用到Pandas的DataFrame数据结构也就总想着初始化一个空的DataFrame,虽然没什么太大...

Python绑定方法与非绑定方法详解

本文实例为大家分享了Python绑定方法与非绑定方法,供大家参考,具体内容如下 定义: 绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入):     1. 绑定到类的方法:用...