溫馨提示×

Python怎么替換字符串中的部分內(nèi)容

小億
145
2024-03-02 13:27:19
欄目: 編程語言

在Python中,可以使用字符串的replace()方法來替換字符串中的部分內(nèi)容。下面是一個示例:

original_string = "Hello, world!"
new_string = original_string.replace("world", "Python")

print(new_string)

上面的代碼將會輸出Hello, Python!,即將原始字符串中的"world"替換為"Python"。您可以根據(jù)實際需求將要替換的內(nèi)容和替換后的內(nèi)容傳遞給replace()方法。

0