python如何刪除字符串中的指定字符

小億
89
2023-12-16 18:43:37

在Python中,可以使用字符串的replace()方法來(lái)刪除指定的字符。

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

輸出結(jié)果:Hell, Wrld!

在上面的例子中,我們使用replace()方法將字符串中的字符o替換為空字符串,從而實(shí)現(xiàn)刪除字符的效果。

0