新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

python中str.strip()如何使用,python bin

发布时间:2023-10-18 13:21:39

python中str.strip()如何使用

str.strip()方法用于去除字符串首尾指定字符,默许去除空格。
使用方法以下:

str.strip([chars])

其中,chars为可选参数,用于指定需要去除的字符。
示例:

s = "  hello world  "
print(s.strip())  # 输出:"hello world"
s = "  hello world  "
print(s.strip(" "))  # 输出:"hello world"
s = "#hello world#"
print(s.strip("#"))  # 输出:"hello world"

请注意,str.strip()方法只能去除字符串首尾的指定字符,中间部份不会被去除。如果需要去除字符串中所有指定字符,可使用str.replace()方法。