python:使用strip()去掉首尾空格

使用内置函数strip去掉首尾空格

1. strip()函数

2. 代码实例

#!/user/bin/env python3
# -*- coding: utf-8 -*-


# 例子1(去掉首尾空格)
str1 = " hello hi andbbddbdbdb "
print (str1.strip( ' ' ))  # 指定字符串:空格(默认就是空格)

# 例子2(去掉指定字符串)
str2 = "*****this is **string** example....wow!!!*****"
print (str2.strip( '*' ))  # 指定字符串:*

# 例子3(去掉指定字符序列)
str3 = "123abcrunoob321"
print (str3.strip( '12' ))  # 指定字符序列为:12


'''
输出结果为:
hello hi andbbddbdbdb
this is **string** example....wow!!!
3abcrunoob3
'''

参考:

https://blog.csdn.net/lanyangyang0103/article/details/89313275  python 去掉首尾空格

猜你喜欢

转载自blog.csdn.net/weixin_39450145/article/details/120008161