Skip to content Skip to sidebar Skip to footer

What Is The Correct Form For String.replace?

Following the Python documentation for string.replace ( http://docs.python.org/library/string.html ): string.replace(str, old, new[, maxreplace]) Return a copy of string str with

Solution 1:

I think your confusion here (and that of most of the answers) is the different between the string module and the str built-in class. They're entirely separate things, even if there is a lot of overlap in functionality.

string.replace(s, old, new) is a free function, not a method. There's no way you can call it as s.replace(old, new), because s cannot be an instance of the string module.

str.replace(self, old, new) is a method. As with any other method (other than classmethod and staticmethod methods), you can—and usually do—call it through a str instance, as s.replace(old, new), where s becomes the self parameter automatically.

You can also call a method through the class, so str.replace(s, old, new) turns out to be exactly the same as s.replace(old, new). And it just so happens that, if s is a str, this does the exact same thing as string.replace(old, new). But that's really a coincidence that's true for historical reasons.

As a side note, you almost never want to call functions in the string module. They're mostly a holdover from very early versions of Python. In fact, string.replace is listed under the "Deprecated string functions" section in the documentation, as are most of the other functions you'd probably go looking for there. The reason the whole module hasn't been deprecated is that it has some things that don't belong in the str (or bytes or unicode) class, such as constants like string.digits.

Solution 2:

Yes, that doc is correct, since it is referring to using string.replace() as a stand alone function. So you can do this:

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

This is different from calling replace() as a method of a given string, like this:

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

They are two different things that have different syntax but are designed to have the same results. So calling one with the other's syntax will result in an error. For example:

>>>'a'.replace('a','a','b')
Traceback (most recent calllast):
  File "<stdin>", line 1, in<module>
TypeError: an integeris required

Solution 3:

Looks like you're confusing the 'replace' method of the string module with the 'replace' method of a python string.

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

will return "test"

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

will return "test"

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

will return the error you mention

Solution 4:

Python methods use an explicit self. That is to say, where C++ / javscript have a magic this variable, Python passes it explicitly as the first argument. When you call the method, the object to the left of the dot becomes the first argument of the method.

These are equivalent:

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

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

You see that there are just four values (and one class: str) involved in both cases.

Solution 5:

Update for latest syntax

for now is 2021 year

the latest official doc

move to here: string replace

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

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

corresponding python code (in VSCode) syntax notice

enter image description here

which syntax is:

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

Answer for: It seems odd that you'd need the "str" repeated

not odd, after you know the details:

  • Python's builtin String Class named: str

-> str.replace(old, new[, count]) has two normal use case:

Use case 1: use builtin string class str

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

Use case 2: use string variable itself

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

Answer for: TypeError: an integer is required

your (error) code:

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

's explanation:

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

match above Use case 2 's syntax:

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

so:

  • 'a' => strVariable
  • 'a' => old
  • 't' => new
  • 'c' => optional count

but 'c' is str, not (expected) int, so report error:

TypeError: an integer is required

How to change my code?

as above said, change your code to:

Use case 1: use builtin string class str

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

Use case 2: use string variable itself

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

Post a Comment for "What Is The Correct Form For String.replace?"