共計(jì) 283 個(gè)字符,預(yù)計(jì)需要花費(fèi) 1 分鐘才能閱讀完成。
Python 中可以使用 replace()
方法來(lái)替換字符串中的某個(gè)字符。
用法如下:
string.replace(old, new, count)
其中,string
是要進(jìn)行替換操作的字符串,old
是要被替換的字符,new
是替換后的字符,count
是可選的參數(shù),表示替換次數(shù),默認(rèn)為替換所有匹配項(xiàng)。
示例:
string = "Hello, world!"
new_string = string.replace("o", "*")
print(new_string)
輸出:
Hell*, w*rld!
在上面的例子中,我們將字符串中的所有 "o" 字符替換為 "*" 字符。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完