1,去除字符串两端相同的子串:str2.lstrip(str1), str2.rstrip(str1), str2.strip(str1),

例如:

>>> str2='xyz12345xyz'
>>> print(str2.lstrip('xyz')+'|'+str2.rstrip('xyz')+'|'+str2.strip('xyz'))
12345xyz|xyz12345|12345

2,另外要去除的子串也可以用正则表达式来写,从而去除某一类型的字串:

例如:去除两端的数字

>>> ss = '123woshi233'
>>> ss.strip('[123456789]')
'woshi'

本文转自https://www.cnblogs.com/star12111/p/8834968.html

相关文章:

  • 2021-11-18
  • 2022-12-23
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-22
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2021-08-15
  • 2022-12-23
相关资源
相似解决方案