Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:28:17+00:00 2026-06-11T16:28:17+00:00

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

  • 0

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 all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.

Using the format as given generates the following error:

>>> a = 'grateful'
>>> a.replace(a,'t','c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required

It seems odd that you’d need the "str" repeated and from the error I guessed that my third argument was being taken for maxreplace.

The format:

string.replace(old, new)

does seem to function as expected.

I’m wondering if I am misunderstanding something, and the form given in the Python documentation is, in fact, correct in some way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:28:18+00:00Added an answer on June 11, 2026 at 4:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a simple example from the python documentation (http://docs.python.org/extending/extending.html): static PyObject * spam_system(PyObject
Relatively new to Python, and I saw the following construct in the PyFacebook library
The following is from the Python v3.1.2 documentation: From The Python Language Reference Section
I think I am following the python documentation correctly but I am having trouble
I have had the following problem which both Google and the Python documentation cannot
I saw the following Python documentation which says that define variables in a Class
The Python documentation claims that the following does not work on some platforms or
Following Python's documentation , I'm trying to override logging.Formatter.converter in order to control the
I'm new to unittesting in python. I tried the unittest example from the documentation:
I am new to Solr. I have been following the documentation provided in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.