python

python中strip怎么使用

小亿
116
2023-09-25 19:32:28
栏目: 编程语言
Python开发者服务器,限时0元免费领! 查看>>

在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"

注意:

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

相关推荐:strip在python中怎么使用

0
看了该问题的人还看了