知行编程网知行编程网  2022-10-20 15:00 知行编程网 隐藏边栏  114 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python的replace无法替换怎么办的相关知识,包括js replace替换最后一个字符,以及python tuple这些编程知识,希望对大家有参考作用。

python的replace不能替换怎么办?

replace替换字符串时,需要重新赋值给变量,因为字符串在python中是不可变的对象,所以我们在使用的时候必须重新赋值,就这么简单。 Python replace() 方法将字符串中的旧(旧字符串)替换为新的(新字符串)。如果指定了第三个参数 max,则替换不会超过 max 次。

replace()方法语法:

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

参数

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

返回值

返回通过用新的(新字符串)替换字符串中的旧(旧字符串)生成的新字符串。如果指定了第三个参数 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

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享