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

相关文章

TensorFlow数据输入的方法示例

TensorFlow数据输入的方法示例

读取数据(Reading data) TensorFlow输入数据的方式有四种: tf.data API:可以很容易的构建一个复杂的输入通道(pipeline)(首选数据输入方式...

Python3enumrate和range对比及示例详解

前言 在Python中,enumrate和range都常用于for循环中,enumrate函数用于同时循环列表和元素,而range()函数可以生成数值范围变化的列表,而能够用于for循环...

python任务调度实例分析

本文实例讲述了python任务调度实现方法。分享给大家供大家参考。具体如下: 方法1: import sched, time import os s = sched.schedule...

详解python多线程之间的同步(一)

详解python多线程之间的同步(一)

引言: 线程之间经常需要协同工作,通过某种技术,让一个线程访问某些数据时,其它线程不能访问这些数据,直到该线程完成对数据的操作。这些技术包括临界区(Critical Section),互...

Python中的map、reduce和filter浅析

1、先看看什么是 iterable 对象 以内置的max函数为例子,查看其doc:复制代码 代码如下:>>> print max.__doc__max(iterable...