共計 366 個字符,預計需要花費 1 分鐘才能閱讀完成。
Python 中可以使用 replace()
函數(shù)來替換字符串的內(nèi)容。replace()
函數(shù)接受兩個參數(shù),第一個參數(shù)是要被替換的字符串,第二個參數(shù)是要替換成的字符串。示例如下:
string = "Hello, World!"
new_string = string.replace("World", "Python")
print(new_string)
輸出結(jié)果為:
Hello, Python!
注意,replace()
函數(shù)返回的是替換后的新字符串,原始字符串不會被修改。如果想要在原始字符串上進行替換,可以直接賦值給原始字符串變量。例如:
string = "Hello, World!"
string = string.replace("World", "Python")
print(string)
輸出結(jié)果為:
Hello, Python!
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完