温馨提示×

python替换用什么函数

九三
252
2021-01-22 10:16:03
栏目: 编程语言

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

0