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

相关文章

用tensorflow实现弹性网络回归算法

本文实例为大家分享了tensorflow实现弹性网络回归算法,供大家参考,具体内容如下 python代码: #用tensorflow实现弹性网络算法(多变量) #使用鸢尾花数据集,...

pytorch中的embedding词向量的使用方法

Embedding 词嵌入在 pytorch 中非常简单,只需要调用 torch.nn.Embedding(m, n) 就可以了,m 表示单词的总数目,n 表示词嵌入的维度,其实词嵌入就...

Python3.5基础之NumPy模块的使用图文与实例详解

Python3.5基础之NumPy模块的使用图文与实例详解

本文实例讲述了Python3.5基础之NumPy模块的使用。分享给大家供大家参考,具体如下: 1、简介 2、多维数组——ndarray...

Python函数式编程指南(四):生成器详解

4. 生成器(generator) 4.1. 生成器简介 首先请确信,生成器就是一种迭代器。生成器拥有next方法并且行为与迭代器完全相同,这意味着生成器也可以用于Python的for循...

python conda操作方法

conda 虚拟环境安装 List item conda env list #查看已安装虚拟环境 coda创建虚拟环境非常方便:官方教程:https://conda.io/project...