Python replace()方法

描述

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

語法

replace()方法語法:python

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

參數

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

返回值

返回字符串中的 old(舊字符串) 替換成 new(新字符串)後生成的新字符串,若是指定第三個參數max,則替換不超過 max 次。xss

實例

如下實例展現了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轉自:http://www.runoob.com/python/att-string-replace.html最近開始學起了python,寫了一個xss掃描器,遇到了不少問題,在接下來的日子裏,我會把這些感悟和看過的資料寫或者裝載到博客,僅僅做爲本身之後查閱的一個途徑。天天學習一點點!
相關文章
相關標籤/搜索