Linux 拨号vps windows公众号手机端

python中strip怎么使用

lewis 7年前 (2018-05-25) 阅读数 7 #程序编程
文章标签 python

在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()方法返回一个新的字符串,原始字符串不会被修改。

  • 如果没有指定参数,默认删除开头和结尾的空格字符。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门