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

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      
posted on 2020-06-23 13:53  杨枫哥  阅读(419)  评论(0编辑  收藏  举报