共計 377 個字符,預計需要花費 1 分鐘才能閱讀完成。
Python 的 strip()
方法用于移除字符串中的指定字符,默認移除字符串兩端的空白字符(包括空格、制表符、換行符等)。strip()
方法的語法如下:
string.strip([chars])
其中,string
是要處理的字符串,chars
是可選參數,用于指定要移除的字符。如果不指定 chars
參數,則默認移除字符串兩端的空白字符。
示例:
string = "Hello, World!"
print(string.strip()) # 輸出: "Hello, World!"
string = "www.example.com"
print(string.strip("w")) # 輸出: ".example.com"
在第一個示例中,strip()
方法移除了字符串兩端的空白字符。在第二個示例中,strip("w")
方法移除了字符串兩端的 ”w” 字符。
丸趣 TV 網 – 提供最優質的資源集合!
正文完