首页 > 编程语言 > 详细

python之字符串strip、rstrip、lstrip的方法

时间:2020-06-23 16:04:25      阅读:55      评论:0      收藏:0      [点我收藏+]

1.描述

strip():用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列

rstrip():用于移除字符串右边指定的字符(默认为空格或换行符)或字符序列

lstrip():用于移除字符串左边指定的字符(默认为空格或换行符)或字符序列

2.语法

str.strip( ‘[chars]‘ )

str.rstrip( ‘[chars]‘ )

str.lstrip( ‘[chars]‘ )

3.参数

参数chars表示需要移除字符串头尾/左边/右边指定的字符序列

4.返回值

返回移除字符串头尾/右边/左边指定的字符生成的新字符串

5.实例

str1 = "000000321456Maple123456000000"
new_str = str1.strip( ‘0‘ )
print(new_str)

str2 = "      Maple123      "
new_rstr = str2.rstrip()
new_lstr = str2.lstrip()
print(new_rstr)
print(new_lstr)

#输出结果如下:
321456Maple123456
      Maple123
Maple123      

python之字符串strip、rstrip、lstrip的方法

原文:https://www.cnblogs.com/Heroge/p/13181707.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!