溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

replace在python中的使用方法

發(fā)布時間:2020-06-17 13:45:39 來源:億速云 閱讀:417 作者:鴿子 欄目:編程語言

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中replace的用法是什么?的詳細內容,更多請關注億速云其它相關文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI