Linux 拨号vps windows公众号手机端

python的strip函数怎么使用

lewis 8年前 (2017-07-03) 阅读数 9 #程序编程
文章标签 python

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

函数语法:strip([chars])

其中,chars参数是可选的,用于指定要去除的字符。如果没有指定chars参数,则默认去除字符串首尾的空格字符。

示例:

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

string = "-----hello, world!-----"
new_string = string.strip('-')
print(new_string)   # 输出:hello, world!

在第一个示例中,strip()函数去除了字符串首尾的空格字符。

在第二个示例中,strip(‘-’)函数去除了字符串首尾的连续的"-"字符。

版权声明

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

发表评论:

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

热门