在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()
方法。