Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1638446
  • 博文数量: 2225
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 22379
  • 用 户 组: 普通用户
  • 注册时间: 2020-11-26 14:30
个人简介

更多python、Linux、网络安全学习内容,可移步:www.oldboyedu.com或关注\"老男孩Linux\"公众号

文章分类

全部博文(2225)

文章存档

2024年(71)

2023年(643)

2022年(693)

2021年(734)

2020年(80)

我的朋友

分类: Python/Ruby

2022-05-11 15:47:18

  在Python编程语言中,有三个常用的“替换”函数,分别是:strip()、replace()、re.sub(),那么Python中replace()的用法是什么?本文为大家详细讲解一下。

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

  语法

  replace()方法语法:

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

  参数

  old -- 将被替换的子字符串;

  new -- 新字符串,用于替换old子字符串;

  max -- 可选字符串,替换不超过max次。

  返回值

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

  实例

  #!/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

阅读(776) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~