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实现简易内存监控

本例主要功能:每隔3秒获取系统内存,当内存超过设定的警报值时,获取所有进程占用内存并发出警报声。内存值和所有进程占用内存记入log,log文件按天命名。 1 获取cpu、内存、进程信息...

Windows下python3安装tkinter的问题及解决方法

最近尝试写python GUI界面,决定先从tkinter开始。 但是遇到了无法安装。执行pip install tkinter没有用,报了如下错误: C:\Users\zhengji...

python下实现二叉堆以及堆排序的示例

堆是一种特殊的树形结构, 堆中的数据存储满足一定的堆序。堆排序是一种选择排序, 其算法复杂度, 时间复杂度相对于其他的排序算法都有很大的优势。 堆分为大头堆和小头堆, 正如其名, 大头堆...

利用python实现.dcm格式图像转为.jpg格式

如下所示: import pydicom import matplotlib.pyplot as plt import scipy.misc import pandas as...

9种python web 程序的部署方式小结

主流的web server 一个巴掌就能数出来,apache,lighttpd,nginx,iis application,中文名叫做应用服务,就是你基于某个web framework写...