教程集 www.jiaochengji.com
教程集 >  Python编程  >  python进阶  >  正文 Python中字符串的strip、lstrip和rstrip

Python中字符串的strip、lstrip和rstrip

发布时间:2021-01-27   编辑:jiaochengji.com
教程集为您提供Python中字符串的strip、lstrip和rstrip等资源,欢迎您收藏本站,我们将为您提供最新的Python中字符串的strip、lstrip和rstrip资源

Python中strip用于去除字符串的首位字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。

这三个参数都可以传入一个参数,指定要去除的首尾字符。

需要注意的是,传入的是一个字符数组,编译器去除两端所有匹配的字符,直到没有匹配的字符,比如:

>>> testString="saaaay yes no yaaaass"
>>> print testString.strip('say')
 yes no 
>>>

可见,testString依次被去除首尾在['s','a','y']数组内的字符,直到剩余字符不再数组内。所以输出yes no。

Note:

当没有传入参数时,默认去除首尾空格。

lstrip和rstrip原理一样。

举例:

>>> testString="saaaay yes no yaaaass"
>>> print testString.strip('say')   
 yes no #以空格开头和结尾的
>>> print testString.strip('say ')
es no#开头结尾均无空格
>>> print testString.lstrip('say')
 yes no yaaaass#以空格开头
>>> print testString.rstrip('say')
saaaay yes no #以空格结尾
>>>

您可能感兴趣的文章:
Python中字符串的strip、lstrip和rstrip
python中strip()函数有什么用法
python怎么去除换行符
你真的了解strip()、lstrip()、rstrip()函数吗?
python如何判断字符串不为空格
Python去除字符串中空格(删除指定字符)的3种方法
python输出怎么不要空格
python怎么删除字符
python如何查看类的函数
python如何去掉空格

[关闭]
~ ~