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

相关文章

跟老齐学Python之画圈还不简单吗?

跟老齐学Python之画圈还不简单吗?

在python中,循环有一个语句:for语句。 简单的for循环例子 >>> hello = "world" >>> for i in hello...

老生常谈Python startswith()函数与endswith函数

函数:startswith() 作用:判断字符串是否以指定字符或子字符串开头 一、函数说明 语法:string.startswith(str, beg=0,end=len(string)...

Python基于matplotlib实现绘制三维图形功能示例

Python基于matplotlib实现绘制三维图形功能示例

本文实例讲述了Python基于matplotlib实现绘制三维图形功能。分享给大家供大家参考,具体如下: 代码一: # coding=utf-8 import numpy as np...

pycharm+django创建一个搜索网页实例代码

pycharm+django创建一个搜索网页实例代码

本文主要研究的是pycharm+django创建一个搜索网页的实例代码,具体步骤和代码示例如下。 创建工程 比如,我创建的工程目录结构如下: 命令行 进入windows命令行,进入根...

Python实现选择排序

选择排序: 选择排序(Selection sort)是一种简单直观的 排序算法 。它的工作原理如下。首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元...