python中的strip函数怎么用
导读:在Python中,strip函数用于去除字符串首尾的指定字符(默认为空格字符)。 strip函数的语法如下: string.strip([chars] 参数说明: `chars`(可选):指定要去除的字符。如果不指定,则默认去除字符串首...
在Python中,strip函数用于去除字符串首尾的指定字符(默认为空格字符)。
strip函数的语法如下:
string.strip([chars])
参数说明:
- `chars`(可选):指定要去除的字符。如果不指定,则默认去除字符串首尾的空格字符。
示例:
string = " hello world "
print(string.strip()) # 输出: "hello world"
string = "---hello world---"
print(string.strip("-")) # 输出: "hello world"
string = "123hello world321"
print(string.strip("123")) # 输出: "hello world"
在上面的示例中,第一个例子是去除字符串`string`首尾的空格字符。第二个例子是去除字符串`string`首尾的连续的`-`字符。第三个例子是去除字符串`string`首尾的连续的`1`、`2`、`3`字符。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: python中的strip函数怎么用
本文地址: https://pptw.com/jishu/569189.html