4

遵循 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 文档中给出的形式实际上在某种程度上是正确的。

4

5 回答 5

7

我认为您在这里的困惑(以及大多数答案的困惑)是string模块和str内置类之间的不同。它们是完全独立的东西,即使功能上有很多重叠。

string.replace(s, old, new)是一个自由函数,而不是一个方法。您无法将其称为s.replace(old, new),因为s不能是string模块的实例。

str.replace(self, old, new)是一种方法。与任何其他方法(除了 classmethod 和 staticmethod 方法)一样,您可以并且通常会通过str实例调用它,因为s.replace(old, new), where自动s成为self参数。

也可以通过类调用方法,str.replace(s, old, new)结果与s.replace(old, new). 碰巧的是,如果s是 a str,它的作用与 完全相同string.replace(old, new)。但由于历史原因,这确实是一个巧合。

附带说明一下,您几乎从不想调用string模块中的函数。它们主要是 Python 早期版本的遗留物。事实上,string.replace它列在文档的“不推荐使用的字符串函数”部分下,您可能会在那里寻找大多数其他函数。整个模块没有被弃用的原因是它有一些不属于str(或bytesunicode)类的东西,比如像string.digits.

于 2012-09-19T19:08:34.707 回答
3

是的,该文档是正确的,因为它指的是string.replace()作为独立功能使用。所以你可以这样做:

>>> import string
>>> string.replace("a","a","b")
'b'

replace()这与作为给定字符串的方法调用不同,如下所示:

>>> 'a'.replace('a','b')
'b'

它们是两种不同的东西,具有不同的语法,但旨在具有相同的结果。所以用另一个的语法调用一个会导致错误。例如:

>>> 'a'.replace('a','a','b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
于 2012-09-19T18:59:35.037 回答
2

看起来您将字符串模块的“替换”方法与 python 字符串的“替换”方法混淆了。

string.replace("rest,"r", "t")

将返回“测试”

"rest".replace("r", "t") 

将返回“测试”

"rest".replace("rest", "r", "t") 

将返回您提到的错误

于 2012-09-19T19:07:12.550 回答
0

Python 方法使用显式的 self。也就是说,在 C++/javscript 有一个魔术this变量的地方,Python 将它显式地作为第一个参数传递。当您调用该方法时,点左侧的对象将成为该方法的第一个参数。

这些是等价的:

str.replace('foobar', 'o', 'O', 1)

'foobar'.replace('o', 'O', 1)

您会看到str在这两种情况下只涉及四个值(和一个类:)。

于 2012-09-19T19:06:56.157 回答
0

更新最新语法

现在是2021

最新的官方文档

移至此处:字符串替换

str.replace(旧,新 [,计数])

返回字符串的副本,其中所有出现的子字符串 old 都替换为 new。如果给定了可选参数 count,则仅替换第一个 count 出现。

对应的python代码(in VSCode)语法说明

在此处输入图像描述

语法是:

str.replace(self: str, old, new, count) -> str

回答:It seems odd that you'd need the "str" repeated

不奇怪,在你知道细节之后:

  • Python 的内置字符串类名为:str

->str.replace(old, new[, count])有两个正常用例:

用例 1:使用内置字符串类str

originalStr = 'grateful'
replacedStr = str.replace(originalStr,'t','c')

用例 2:使用string variable自身

originalStr = 'grateful'
replacedStr = originalStr.replace('t','c')

回答:TypeError: an integer is required

您的(错误)代码:

a = 'grateful'
a.replace(a,'t','c')

的解释:

a.replace(a,'t','c')

匹配上述用例 2的语法:

strVariable.replace(old, new[, count])

所以:

  • 'a'=>strVariable
  • 'a'=>old
  • 't'=>new
  • 'c'=> 可选count

但是'c'str,而不是(预期的)int,所以报错:

TypeError:需要一个整数

如何更改我的代码?

如上所述,将您的代码更改为:

用例 1:使用内置字符串类str

originalStr = 'grateful'
replacedStr = str.replace(originalStr,'t','c')

用例 2:使用string variable自身

originalStr = 'grateful'
replacedStr = originalStr.replace('t','c')
于 2021-04-08T05:28:47.093 回答