共計 409 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Python 中,strip() 是一個字符串方法,用于刪除字符串的開頭和結尾的指定字符(默認是空格字符)。
使用方法如下:
- 刪除字符串開頭和結尾的空格字符:
string = "hello world"
result = string.strip()
print(result) # 輸出: "hello world"
- 刪除字符串開頭和結尾的指定字符:
string = "!!!hello world!!!"
result = string.strip("!")
print(result) # 輸出: "hello world"
- 刪除字符串開頭和結尾的多個指定字符:
string = "!!!hello world!!!"
result = string.strip("!")
print(result) # 輸出: "hello world"
注意:
- strip() 方法返回一個新的字符串,原始字符串不會被修改。
- 如果沒有指定參數,默認刪除開頭和結尾的空格字符。
丸趣 TV 網 – 提供最優質的資源集合!
正文完