Linux 拨号vps windows公众号手机端

python替换用什么函数

lewis 6年前 (2019-03-20) 阅读数 12 #程序编程
文章标签 python

python中使用replace函数进行替换,具体方法如下:

replace:replace()函数的作用是把字符串中的old替换成 new。

replace()函数语法:

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

参数:

old:将被替换的子字符串。

new:新字符串,用于替换old子字符串。

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

replace()函数使用方法:

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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门