Python_Strip()方法


Python strip([chars]) 方法用于移除字符串头尾指定的字符(默认为空格)。

看了用法的解释还是不太明白,直接运行几个例子后就清晰了。

举例:

freshfruit = [' banana', ' loganberry', ' passion fruit ']

print([weapon.strip() for weapon in freshfruit])
--》['banana', 'loganberry', 'passion fruit']  # 功能:去空格
print([weapon.strip(' ba') for weapon in freshfruit])  # 注意有空格
--》['nan', 'loganberry', 'passion fruit']
print([weapon.strip('ba') for weapon in freshfruit])  # 注意无空格
--》[' banan', ' loganberry', ' passion fruit ']
print([weapon.strip('a') for weapon in freshfruit])
--》[' banan', ' loganberry', ' passion fruit ']

拿出字符串' banana'的首字符-空格和尾字符-a去chars中匹配(例如‘ ba’),若首尾字符在chars中可以找到则删除该字符





免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM