新聞中心
REPLACE替換字符串的時(shí)候,需要重新賦值給變量,因?yàn)樵趐ython中字符串是不可變對(duì)象,因此在使用的時(shí)候我們必須重新賦值,就這么簡(jiǎn)單。Python replace() 方法把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個(gè)參數(shù)max,則替換不超過 max 次。

葫蘆島網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)公司從2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
replace()方法語(yǔ)法:
str.replace(old, new[, max])
參數(shù)
old -- 將被替換的子字符串。 new -- 新字符串,用于替換old子字符串。 max -- 可選字符串, 替換不超過 max 次
返回值
返回字符串中的 old(舊字符串) 替換成 new(新字符串)后生成的新字符串,如果指定第三個(gè)參數(shù)max,則替換不超過 max 次。
以下實(shí)例展示了replace()函數(shù)的使用方法:
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);輸出結(jié)果
thwas was string example....wow!!! thwas was really string thwas was string example....wow!!! thwas is really string
更多學(xué)習(xí)內(nèi)容,請(qǐng)點(diǎn)擊Python學(xué)習(xí)網(wǎng)。
標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:python的replace無法替換怎么辦
文章來源:http://m.fisionsoft.com.cn/article/coseigp.html


咨詢
建站咨詢
