共計 329 個字符,預計需要花費 1 分鐘才能閱讀完成。
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() 方法。
丸趣 TV 網 – 提供最優質的資源集合!
正文完