strip在python中怎么使用
在Python中,strip()
函数用于去除字符串两端的指定字符(默认为空格)或空白符。可以使用以下方式来使用strip()
函数:
- 使用默认的空格字符去除两端空格:
s = " hello world "
print(s.strip()) # Output: "hello world"
- 指定要去除的字符:
s = "!!!hello world!!!"
print(s.strip("!")) # Output: "hello world"
- 去除指定字符集合:
s = "!!!hello world!!!"
print(s.strip("!d")) # Output: "hello worl"
需要注意的是,strip()
函数会返回一个新的字符串,原始字符串不会被修改。
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:云服务器的环境如何配置 下一篇:golang责任链模式有哪些特点
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。