1.描述

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

2.语法

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

3.参数

参数 备注
old 将要被替换的子字符串或字符序列
new 将要替换成的新的字符串或字符序列
max 可选参数,替换不超过max次

4.返回值

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

5.实例

str1 = "Hao123yf456yf789yang234yf8902"
new_str = str1.replace('yf','Maple',2)
print(new_str)

#输出结果如下:
Hao123Maple456Maple789yang234yf8902

相关文章:

  • 2022-12-23
  • 2021-11-06
  • 2022-01-30
  • 2021-07-19
  • 2022-12-23
  • 2021-10-17
  • 2022-12-23
  • 2021-10-24
猜你喜欢
  • 2022-12-23
  • 2021-09-17
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-16
相关资源
相似解决方案