溫馨提示×

python如何替換字符串的內容

小億
116
2023-10-31 16:25:45
欄目: 編程語言

Python中可以使用replace()函數來替換字符串的內容。replace()函數接受兩個參數,第一個參數是要被替換的字符串,第二個參數是要替換成的字符串。示例如下:

string = "Hello, World!"
new_string = string.replace("World", "Python")
print(new_string)

輸出結果為:

Hello, Python!

注意,replace()函數返回的是替換后的新字符串,原始字符串不會被修改。如果想要在原始字符串上進行替換,可以直接賦值給原始字符串變量。例如:

string = "Hello, World!"
string = string.replace("World", "Python")
print(string)

輸出結果為:

Hello, Python!

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女