永发信息网

很奇怪 为什么python的replace没有替换成功

答案:2  悬赏:30  手机版
解决时间 2021-02-03 22:04
  • 提问者网友:心牵心
  • 2021-02-02 23:39
很奇怪 为什么python的replace没有替换成功
最佳答案
  • 五星知识达人网友:一叶十三刺
  • 2021-02-03 00:26
Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。


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



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


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


示例:

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
全部回答
  • 1楼网友:封刀令
  • 2021-02-03 01:20
python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。 语法 str.replace(old, new[, max]) 参数 old -- 将被替换的子字符串。 new -- 新字符串,用于替换old子字符串。 max -- 可选字符串, 替换不超过 max 次 返回值 返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。 示例: 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 stringthwas was string example....wow!!! thwas is really string
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯