Python字符串方法strip
2021/11/11 9:39:38
本文主要是介绍Python字符串方法strip,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
方法strip将字符串开头和末尾的空白(但不包括中间的空白)删除,并返回删除后的结果。
' internal whitespace is kept '.strip()
输出结果如下所示:
'internal whitespace is kept'
与lower一样,需要将输入与存储的值进行比较时,strip很有用。回到前面介绍lower时使用的用户名示例,并假定用户输入用户名时不小心在末尾加上了一个空格。
names = ['gumby', 'smith', 'jones'] name = 'gumby ' if name in names: print('Found it!') if name.strip() in names: print('Found it!')
输出结果如下所示:
Found it!
还可在一个字符串参数中指定要删除哪些字符。
>>> '*** SPAM * for * everyone!!! ***'.strip(' *!')
输出结果如下所示:
'SPAM * for * everyone'
这个方法只删除开头或末尾的指定字符,因此中间的星号未被删除。
这篇关于Python字符串方法strip的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python