Python中可以使用replace()方法來替換字符串中的某個字符。
用法如下:
string.replace(old, new, count)
其中,string是要進行替換操作的字符串,old是要被替換的字符,new是替換后的字符,count是可選的參數,表示替換次數,默認為替換所有匹配項。
示例:
string = "Hello, world!"
new_string = string.replace("o", "*")
print(new_string)
輸出:
Hell*, w*rld!
在上面的例子中,我們將字符串中的所有"o"字符替換為"*"字符。