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扫描器,遇到了不少问题,在接下来的日子里,我会把这些感悟和看过的资料写或者装载到博客,仅仅做为本身之后查阅的一个途径。天天学习一点点!
相关文章
相关标签/搜索