python中replace的用法是什么?-Python教程

资源魔 38 0

python中replace的用法是甚么?

Python replace() 办法把字符串中的 old(旧字符串) 交换成 new(新字符串),假如指定第三个参数max,则交换没有超越 max 次。

replace()办法语法:

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

参数:

old -- 将被交换的子字符串。new -- 新字符串,用于交换old子字符串。max -- 可选字符串, 交换没有超越 max 次

前往值:

前往字符串中的 old(旧字符串) 交换成 new(新字符串)后天生的新字符串,假如指定第三个参数max,则交换没有超越 max 次。

如下实例展现了replace()函数的应用办法:

#!/usr/bin/python
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

保举教程:《python教程》

以上就是python中replace的用法是甚么?的具体内容,更多请存眷资源魔其它相干文章!

标签: Python python教程 python编程 python使用问题 replace

抱歉,评论功能暂时关闭!