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 Django框架单元测试之文件上传测试示例

Python Django框架单元测试之文件上传测试示例

本文实例讲述了Python Django框架单元测试之文件上传测试。分享给大家供大家参考,具体如下: Submitting files is a special case. To POS...

对pycharm 修改程序运行所需内存详解

编辑PyCharm安装目录下PyCharm 4.5.3\bin下的pycharm.exe.vmoptions文件, 如下 -server -Xms128m -Xmx512m -XX:...

python组合无重复三位数的实例

# -*- coding: utf-8 -*- # 简述:这里有四个数字,分别是:1、2、3、4 #提问:能组成多少个互不相同且无重复数字的三位数?各是多少? def f(n):...

python如何读取bin文件并下发串口

下面是实现代码 # coding:utf-8 import time, serial from struct import * import binascii file = ope...

Python实现获取操作系统版本信息方法

最近,想在我的YouMoney(http://code.google.com/p/youmoney/)里面增加提取用户操作系统版本信息。比如windows用户,可能要返回Windows...