Last Updated: Saturday 21st December 2013

Replacing Python Strings

Often you'll have a string (str object), where you will want to modify the contents by replacing one piece of text with another. In Python, everything is an object - including strings. This includes the str object. Luckily, Python's string module comes with a replace() method. The replace() method is part of the string module, and can be called either from a str object or from the string module alone.

Python's string.replace() Prototype

The prototype of the string.replace() method is as follows:

string.replace(s, old, new[, maxreplace])

Function parameters
  • s: The string to search and replace from.
  • old: The old sub-string you wish to replace.
  • new: The new sub-string you wish to put in-place of the old one.
  • maxreplace: The maximum number of times you wish to replace the sub-string.


From the string Module Directly

This gives us the following output:

And using the string.replace() method from the str object:

Which gives us:

Shockingly, we get the same output.

And there you have it! Python's string.replace().

To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.

  • extinctplanet

    What about:
    str = ‘\something\’
    str.replace(‘\’, ”)

    Traceback ( File “”, line 1
    str.replace(‘\’, ”)
    SyntaxError: EOL while scanning string literal

    • The offending code is ”

      When you use a backspace character followed by a quote, it escapes the quote, so you can insert quotes within quotes. In that case, it should be ”’

    • Jake Griffin

      When you say:
      my_str = ‘\something\’ # Sidenote: It is bad practice to override the keyword “str” in python.
      the contents of my_str is actually already something, because the first escapes the second at the start and end of the string. To put \something\ into a string, you need to use:
      my_str = “\\something\\”
      or even better (r denotes a ‘raw string’ in python):
      my_str = r”\something\”

      Then, to do the replacement:
      my_str.replace(r”\”, r””) # or my_str.replace(“\\”, “\”) if you prefer

  • moomistercowman

    string.replace(s, old, new[, count]) is deprecated according to pydoc. It is better to just use str_object.replace() going forward. Using object.replace() avoids both an unnecessary module import (for .replace()) *and* gives 2to3 less to modify.

  • Bob

    I can’t seem to replace something with the contents of a variable.
    value = ‘goodbye’
    myString = ‘goodbye world’
    str.replace(myString, value, ‘hello’)

    • Bob


      value = ‘hello’
      myString = ‘goodbye world’
      str.replace(myString, ‘goodbye’, value)

      • Hi Bob, strings are immutable in Python. So the str.replace() method returns the replaced string, but doesn’t modify the arguments themselves. eg.

        my_replaced_str = str.replace(myString, 'goodbye', value)

        However as moomistercowman said, string.replace() is being deprecated, so the syntax would be myString.replace('goodbye', 'hello')

  • how to replace double quotes by null in the UTF-16?
    it doesn’t work: string.replace(line, u”u0022″, u”u0000″)

  • venkat

    How can I replace data for variables, Suppose I have “username=user1, [email protected],…
    username=user2, [email protected],….”
    I want to mask user1, user2 values with some string like username=xxxxx, … username=xxxxx

  • Chintan Patel

    Thanks for this