python的strip()函数和rstrip()函数的用法总结

您所在的位置:网站首页 python中lstrip函数的用法 python的strip()函数和rstrip()函数的用法总结

python的strip()函数和rstrip()函数的用法总结

2024-03-19 19:57| 来源: 网络整理| 查看: 265

strip函数原型

声明:s为字符串,rm为要删除的字符序列. 只能删除开头或是结尾的字符或是字符串。不能删除中间的字符或是字符串。

s.strip(rm)        删除s字符串中开头、结尾处,位于 rm删除序列的字符

s.lstrip(rm)       删除s字符串中开头处,位于 rm删除序列的字符

s.rstrip(rm)      删除s字符串中结尾处,位于 rm删除序列的字符

注意:

1. 当rm为空时,默认删除空白符(包括'\n', '\r',  '\t',  ' ')

例如:

2.这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。

例如 :

rstrip() 删除 string 字符串末尾的指定字符(默认为空格).

语法

rstrip()方法语法:

str.rstrip([chars])

参数

chars -- 指定删除的字符(默认为空格)

返回值

返回删除 string 字符串末尾的指定字符后生成的新字符串。

实例

以下实例展示了rstrip()函数的使用方法:

#!/usr/bin/python

str = "     this is string example....wow!!!     "; print str.rstrip(); str = "88888888this is string example....wow!!!8888888"; print str.rstrip('8');

以上实例输出结果如下:

     this is string example....wow!!! 88888888this is string example....wow!!!



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3