新闻资讯

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

< 返回新闻资讯列表

python中的strip函数如何用,python中strip函数怎么用

发布时间:2023-10-27 13:27:06

python中的strip函数如何用

在Python中,strip函数用于去除字符串首尾的指定字符(默许为空格字符)。

strip函数的语法以下:

string.strip([chars])

参数说明:

  • chars(可选):指定要去除的字符。如果不指定,则默许去除字符串首尾的空格字符。

示例:

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

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

string = "123hello world321"
print(string.strip("123"))  # 输出: "hello world"

在上面的示例中,第一个例子是去除字符串string首尾的空格字符。第二个例子是去除字符串string首尾的连续的-字符。第三个例子是去除字符串string首尾的连续的123字符。