遵循string.replace(http://docs.python.org/library/string.html)的Python文档:
string.replace(str,old,new [,maxreplace])
返回字符串str的副本,其中所有出现的substring 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
Run Code Online (Sandbox Code Playgroud)
看起来很奇怪你需要重复"str",并且从错误中我猜到我的第三个参数被用于maxreplace.
格式:
string.replace(旧的,新的)
确实似乎按预期运作.
我想知道我是否误解了某些东西,而且Python文档中给出的形式实际上在某种程度上是正确的.