3

3.7Python字符串strip方法

Posted by 撒得一地 on 2016年2月6日 in python教程

strip方法返回去除字符串两侧(不包括内部)空格的字符串,和PHP中的trim函数一样,实例:

>>> " hehe,my mood is good!   ".strip()
'hehe,my mood is good!'

>>> " front input is useless !!   ".strip()
'front input is useless !!'

strip这个函数经常和lower方法一起使用来对比用户的输入和存储的值。

也可以使用strip方法指定需要去除的字符串,将它们列为参数即可。比如:

>>> '***** spam comment for blog!***!'.strip('*!')
' spam comment for blog'

要注意的是这个方法只会去除两侧的字符,字符串中匹配的字符是不会被去掉的。

上一篇:

下一篇:

相关推荐

3 Comments

发表评论

电子邮件地址不会被公开。 必填项已用*标注

8 + 0 = ?

网站地图|XML地图

Copyright © 2015-2024 技术拉近你我! All rights reserved.
闽ICP备15015576号-1 版权所有©psz.