码迷,mamicode.com
首页 > 编程语言 > 详细

Python replace()方法

时间:2015-10-15 09:54:49      阅读:558      评论:0      收藏:0      [点我收藏+]

标签:

描述

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

转自:http://www.runoob.com/python/att-string-replace.html
最近开始学起了python,写了一个xss扫描器,遇到了很多问题,在接下来的日子里,我会把这些感悟和看过的资料写或者装载到博客,仅仅作为自己以后查阅的一个途径。
每天学习一点点!

Python replace()方法

标签:

原文地址:http://www.cnblogs.com/Heart-J/p/4881344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!