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 实现单通道转3通道

下面有两种方法都可以: import numpy as np a=np.asarray([[10,20],[101,201]]) # a=a[:,:,np.newaxis] # p...

Python数据结构与算法之常见的分配排序法示例【桶排序与基数排序】

本文实例讲述了Python数据结构与算法之常见的分配排序法。分享给大家供大家参考,具体如下: 箱排序(桶排序) 箱排序是根据关键字的取值范围1~m,预先建立m个箱子,箱排序要求关键字类型...

使用Python实现跳一跳自动跳跃功能

使用Python实现跳一跳自动跳跃功能

1.   OpenCV:模板匹配。    获得小跳棋中心位置 2.   OpenCV:边缘检测。 &nbs...

解决pycharm运行程序出现卡住scanning files to index索引的问题

有时候会出现索引问题,显示scanning files to index 解决方法: in pycharm, go to the "File" on the left top, then...

Python3 实现串口两进程同时读写

通过两个进程分别读写串口,并把发送与接收到的内容记录在blog中,收到q时程序结束并退出 import threading,time import serial import str...