python strip()函数 介绍

来源:互联网 发布:阿萨辛捏脸数据 编辑:程序博客网 时间:2024/05/20 12:50

最近有个需求操作字符串,具体需要如下:

text="<div>  文字  </div>"  text也有可能为 "<p>  文字2 </p>"

现在需要将<div></div>或者<p></p>去除,留下中间的“文字”

用字符串自带的strip()很好的解决了我的问题。


 >>> text="<div>  文字  </div>">>>>>> print text.strip('</divp>')  文字>>>>>> text2="<p>  文字2  </p>">>>>>> print text2.strip('</divp>')  文字2


<div>、</div>、<p>和</p>都包含在 </divp>中,所以都去掉了。


再简单介绍下,lstrip, 去掉左边的字符(开头)
rstrip, 去掉右边的字符(结尾)

0 0