replace()方法返回當前old換成new,可選擇的替代限制到最大數量的字符串的副本。
語法
以下是replace()方法的語法:
str.replace(old, new[, max])
參數
返回值
此方法返回字符串的拷貝與舊子串出現的所有被新的所取代。如果可選參數最大值給定,只有第一個計數發生替換。
例子
下面的示例演示了replace()方法的使用。
#!/usr/bin/pythonstr = "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 stringthwas was string example....wow!!! thwas is really string
新聞熱點
疑難解答
圖片精選