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

相关文章

pytorch加载自定义网络权重的实现

在将自定义的网络权重加载到网络中时,报错: AttributeError: 'dict' object has no attribute 'seek'. You can only tor...

基于Django用户认证系统详解

一. 认证系统概要 create_user 创建用户 authenticate 验证登录 login 记住用户的登录状态 logout 退出登录 is_authenticated 判断用...

Python中实现变量赋值传递时的引用和拷贝方法

iamlaosong文 曾经看到这样一个问题,一个字典中的元素是列表,将这个列表元素赋值给一个变量,然后修改这个列表中元素的值,结果发现,字典中那个列表也同样修改了。 那个问题如下:...

python 实现提取某个索引中某个时间段的数据方法

如下所示: from elasticsearch import Elasticsearch import datetime import time import dateutil.p...

python通过robert、sobel、Laplace算子实现图像边缘提取详解

python通过robert、sobel、Laplace算子实现图像边缘提取详解

实现思路:   1,将传进来的图片矩阵用算子进行卷积求和(卷积和取绝对值)   2,用新的矩阵(与原图一样大小)去接收每次的卷积和的值   3,卷积图片所有的像素点后,把新的矩阵数据类型...