python中的strip函数怎么用

34次阅读
没有评论

共计 419 个字符,预计需要花费 2 分钟才能阅读完成。

在 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 字符。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计419字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)