wanlifeipeng

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::

s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的所有字符,但只要遇到非rm序列中的字符就停止
s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的所有字符,,但只要遇到非rm序列中的字符就停止
s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的所有字符,,但只要遇到非rm序列中的字符就停止

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

>>> st.rstrip()
'\n\t hello world'
>>> st.lstrip()
'hello world \n\t\r'
>>> st.strip()
'hello world'
>>> xml_tag = '<some_tag>'
>>> xml_tag.lstrip("<")
'some_tag>'
>>> xml_tag.lstrip(">")
'<some_tag>'
>>> xml_tag.rstrip(">")
'<some_tag'
>>> xml_tag.rstrip("<")
'<some_tag>'
>>> xml_tag.strip("<").strip(">")
'some_tag'
>>> xml_tag.strip("<>") #删除开头和结尾的<>
'some_tag'
>>> gt_lt_str = "<><>gt lt str<><><>"
>>> gt_lt_str.strip("<>")
'gt lt str'
>>> gt_lt_str.strip("><") #删除指定序列中的字符,与排列顺序无关
'gt lt str'
>>> foo_str = "<foooooo>blash<foo>"
>>> foo_str.strip("<foo>")
'blash'
>>> foo_str.strip("foo") #虽然字符串中包含foo,但是开头遇到非删除序列的<,结尾遇到非删除序列的>,删除工作就停止了
'<foooooo>blash<foo>'

 

posted on 2017-04-13 17:23  wanlifeipeng  阅读(248)  评论(0编辑  收藏  举报