遵循 string.replace 的 Python 文档(http://docs.python.org/library/string.html):
string.replace(str, old, new[, maxreplace])
返回字符串 str 的副本,其中所有出现的子字符串 old 都替换为 new。如果给出了可选参数 maxreplace,则替换第一个 maxreplace 出现。
使用给定的格式会产生以下错误:
>>> a = 'grateful'
>>> a.replace(a,'t','c')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
您需要重复“str”似乎很奇怪,并且从错误中我猜想我的第三个参数被用于 maxreplace。
格式:
string.replace(旧的,新的)
似乎确实按预期运行。
我想知道我是否误解了某些东西,而 Python 文档中给出的形式实际上在某种程度上是正确的。