共計 280 個字符,預計需要花費 1 分鐘才能閱讀完成。
Python 中的 replace 函數用于將字符串中的指定子字符串替換為另一個字符串。
基本語法如下:
string.replace(old, new, count)
參數說明:
- old:需要被替換的子字符串。
- new:替換后的新字符串。
- count(可選):指定替換的次數,默認為全部替換。
示例:
string = "Hello, World!"
new_string = string.replace("Hello", "Hi")
print(new_string)
輸出結果為:
Hi, World!
注意:replace 函數返回一個新的字符串,原字符串不會被修改。
丸趣 TV 網 – 提供最優質的資源集合!
正文完