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>'

 

相关文章:

  • 2021-07-17
  • 2021-06-24
  • 2021-08-29
  • 2021-10-13
  • 2021-07-18
  • 2021-12-05
  • 2022-01-19
  • 2022-12-23
猜你喜欢
  • 2022-01-05
  • 2021-11-17
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案