首頁  >  文章  >  後端開發  >  python中replace的用法是什麼?

python中replace的用法是什麼?

烟雨青岚
烟雨青岚原創
2020-06-17 09:27:1011364瀏覽

python中replace的用法是什麼?

python中replace的用法是什麼?

Python replace() 方法把字串中的old(舊字串) 替換成new(新字串),如果指定第三個參數max,則替換不超過max 次。

replace()方法語法:

#str.replace(old, new[, max])

參數:

old -- 將被取代的子字串。 new -- 新字串,用於替換old子字串。 max -- 可選字串, 取代不超過max 次

傳回值:

#傳回字串中的old(舊字串) 替換成new(新字串)後產生的新字串,如果指定第三個參數max,則替換不超過max 次。

以下實例展示了replace()函數的使用方法:

#!/usr/bin/python
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);

以上實例輸出結果如下:

thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string

推薦教學:《python教學

以上是python中replace的用法是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn