首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >[115]python replace()方法

[115]python replace()方法

作者头像
周小董
发布于 2022-04-12 19:30:16
发布于 2022-04-12 19:30:16
2K0
举报
文章被收录于专栏:python前行者python前行者

描述

Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

语法

replace()方法语法:

代码语言:javascript
复制
str.replace(old, new[, max])

参数

old – 将被替换的子字符串。 new – 新字符串,用于替换old子字符串。 max – 可选字符串, 替换不超过 max 次

返回值

返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。

实例

以下实例展示了replace()函数的使用方法:

代码语言:javascript
复制
#!/usr/bin/python

str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);

以上实例输出结果如下:

代码语言:javascript
复制
thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/06/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 描述
  • 语法
  • 参数
  • 返回值
  • 实例
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档