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

相关文章

安装PyInstaller失败问题解决

安装PyInstaller失败问题解决

这篇文章主要介绍了安装PyInstaller失败问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 pip install P...

Python Unittest根据不同测试环境跳过用例的方法

Python Unittest根据不同测试环境跳过用例的方法

前言 在利用单元测试框架执行测试用例的过程中,有时只需要执行一部分用例,或者跳过某些暂不需要执行的用例,python的unittest框架就内置这样的功能。 本文章会讲述以下几个内容:...

Python程序中使用SQLAlchemy时出现乱码的解决方案

今天对clubot进行了升级, 但是导入数据后中文乱码, 一开是找资料说是在创建引擎的时候添加编码信息: engine = create_engine("mysql://root:@...

python网络编程学习笔记(八):XML生成与解析(DOM、ElementTree)

xml.dom篇     DOM是Document Object Model的简称,XML 文档的高级树型表示。该模型并非只针对 Python,而是一种普通...

python画图--输出指定像素点的颜色值方法

如下所示: # -*- coding: utf-8 -*- #------------------------------------------------------------...