共計 279 個字符,預計需要花費 1 分鐘才能閱讀完成。
在 Python 中,可以使用字符串的 strip()
方法去除文本的空格。strip()
方法會刪除字符串開頭和結尾的空格。
示例代碼如下:
text = " hello world "
new_text = text.strip()
print(new_text) # 輸出結果為 "hello world"
如果要刪除字符串中所有的空格,可以使用 replace()
方法將空格替換為空字符串。
示例代碼如下:
text = " hello world "
new_text = text.replace(" ", "")
print(new_text) # 輸出結果為 "helloworld"
丸趣 TV 網 – 提供最優質的資源集合!
正文完