Python strip lstrip rstrip使用方法

yipeiwu_com6年前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使用Matplotlib绘制分段函数

python使用Matplotlib绘制分段函数

本文实例为大家分享了python使用Matplotlib绘制分段函数的具体代码,供大家参考,具体内容如下 环境 Python3 Mac OS 代码 # coding:utf-8...

python绘制BA无标度网络示例代码

python绘制BA无标度网络示例代码

如下所示: #Copyright (c)2017, 东北大学软件学院学生 # All rightsreserved #文件名称:a.py # 作 者:孔云 #问题描述: #问题分析...

攻击者是如何将PHP Phar包伪装成图像以绕过文件类型检测的(推荐)

攻击者是如何将PHP Phar包伪装成图像以绕过文件类型检测的(推荐)

在US BlackHat 2018大会上,安全人员证明,攻击者不仅可以利用PHAR包发动RCE攻击,而且,通过调整其二进制内容,他们还可以将其伪装成一幅图像,从而绕过安全检查。 在本文中...

python实现通过shelve修改对象实例

本文实例讲述了python实现通过shelve修改对象的方法,分享给大家供大家参考。 具体实现方法如下: import shelve she = shelve.open('try.s...

在Python中使用全局日志时需要注意的问题

在使用 uliweb 开发 soap webservice 后,启动 uliweb 时,werkzeug 的日志莫名其妙丢失了。 正常的日志: 复制代码 代码如下:[INFO] ...