python中strip怎么使用

46次阅读
没有评论

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

在 Python 中,strip() 是一个字符串方法,用于删除字符串的开头和结尾的指定字符(默认是空格字符)。
使用方法如下:

  1. 删除字符串开头和结尾的空格字符:
string = "hello world"
result = string.strip()
print(result)  # 输出: "hello world"
  1. 删除字符串开头和结尾的指定字符:
string = "!!!hello world!!!"
result = string.strip("!")
print(result)  # 输出: "hello world"
  1. 删除字符串开头和结尾的多个指定字符:
string = "!!!hello world!!!"
result = string.strip("!")
print(result)  # 输出: "hello world"

注意:

  • strip() 方法返回一个新的字符串,原始字符串不会被修改。
  • 如果没有指定参数,默认删除开头和结尾的空格字符。

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

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