python中str.strip()怎么使用

   2025-02-15 2660
核心提示:str.strip()方法用于去除字符串首尾指定字符,默认去除空格。使用方法如下:str.strip([chars])其中,chars为可选参数,用于指定

str.strip()方法用于去除字符串首尾指定字符,默认去除空格。

使用方法如下:

str.strip([chars])

其中,chars为可选参数,用于指定需要去除的字符。

示例:

s = "  hello world  "print(s.strip())  # 输出:"hello world"s = "  hello world  "print(s.strip(" "))  # 输出:"hello world"s = "#hello world#"print(s.strip("#"))  # 输出:"hello world"

请注意,str.strip()方法只能去除字符串首尾的指定字符,中间部分不会被去除。如果需要去除字符串中所有指定字符,可以使用str.replace()方法。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言