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脚本生成caffe train_list.txt的方法

首先给出代码: import os path = "/home/data//" path_exp = os.path.expanduser(path) classes =...

利用Python的sympy包求解一元三次方程示例

环境说明:Python3.7.2+Jupyter Notebook 示例1(求解一元三次方程): import sympy as sp # 导入sympy包 x = sp.Symb...

浅谈python日志的配置文件路径问题

如下所示: import logging import logging.config logging.config.fileConfig(path) logger = logging...

Python判断文本中消息重复次数的方法

本文实例讲述了Python判断文本中消息重复次数的方法。分享给大家供大家参考,具体如下: #coding:gbk ''' Created on 2012-2-3 从文件中读取文本,并...

详解字典树Trie结构及其Python代码实现

字典树(Trie)可以保存一些字符串->值的对应关系。基本上,它跟 Java 的 HashMap 功能相同,都是 key-value 映射,只不过 Trie 的 key 只能是字符...