Python Reference Library →

Python String replace() Method – Replace Substrings Easily

Let’s look at string replace() method – replace substrings easily

  1. Notice

    replace() returns text with matching substrings replaced. A count limits the number of replacements.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'tea tea'
result = text.replace("tea", "coffee", 1)
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea tea'Object 2 · str'coffee tea'
This step

replace() returns text with matching substrings replaced. A count limits the number of replacements.

Picture string replace() method – replace substrings easily

replace() returns text with matching substrings replaced. A count limits the number of replacements.

Image brief

Alt text
A diagram for Python String replace() Method – Replace Substrings Easily. Instructions: text = 'tea tea' result = text.replace("tea", "coffee", 1) print(result). Displayed result: coffee tea. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'tea tea'; object 2: 'coffee tea'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String replace() Method – Replace Substrings Easily". Teaching focus: replace() returns text with matching substrings replaced. A count limits the number of replacements. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'tea tea' result = text.replace("tea", "coffee", 1) print(result) | v DISPLAYED RESULT coffee tea FINAL BINDINGS text --> object 1 (str) result --> object 2 (str) object 1: 'tea tea' object 2: 'coffee tea' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String replace() Method – Replace Substrings Easily. Instructions: text = 'tea tea' result = text.replace("tea", "coffee", 1) print(result). Displayed result: coffee tea. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'tea tea'; object 2: 'coffee tea'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'tea tea'
result = text.replace("tea", "coffee", 1)
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea tea'Object 2 · str'coffee tea'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
coffee tea

What do you expect this program to print?

What does this example display?

text = 'tea tea'
result = text.replace("tea", "coffee", 1)
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

coffee tea

Follow the printed values in order. The output is: coffee tea

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

replace() returns text with matching substrings replaced. A count limits the number of replacements.

replace() returns text with matching substrings replaced. A count limits the number of replacements.

More examples and reference

Python String replace() Method

The replace() method in Python is used to replace parts of a string with another string. It’s a very useful and beginner-friendly tool for text processing.

Syntax

string.replace(old, new, count)

Parameters:

  • old – The substring you want to replace.
  • new – The substring you want to replace it with.
  • count (optional) – The number of times you want to replace old with new. If omitted, all occurrences are replaced.

Returns:

  • A new string with replacements applied. The original string is not modified.

Example 1: Basic Replacement

text = "I love apples"
new_text = text.replace("apples", "bananas")
print(new_text)
I love bananas

Example 2: Replace All Occurrences

quote = "ha ha ha!"
print(quote.replace("ha", "ho"))
ho ho ho!

Example 3: Replace Only First Occurrence

data = "one one one"
print(data.replace("one", "two", 1))
two one one

Use Cases of replace()

  • Correcting typos in strings
  • Formatting text
  • Removing unwanted characters (e.g., replace(",", "") to remove commas)
  • Replacing sensitive or restricted words

Common Mistakes

  • Strings are immutable: replace() returns a new string – it does not change the original.
  • Trying to replace a substring that doesn't exist has no effect (no error).
  • Remember that it's case-sensitive: "hello".replace("H", "J") does nothing.

Interview Tip

Use replace() when cleaning strings or doing simple data transformations. It’s often seen in string manipulation questions or during preprocessing.

Summary

  • replace() is used to replace parts of a string with a new value.
  • You can specify how many times to replace with the optional count parameter.
  • Returns a new string; the original remains unchanged.

Practice Problem

Write a program to replace all spaces in a sentence with dashes -.

sentence = input("Enter a sentence: ")
print(sentence.replace(" ", "-"))

What to remember

Connect what you noticed in the example with the idea below.

Remember

replace() returns text with matching substrings replaced. A count limits the number of replacements.

Watch out for this idea

replace() modifies the original string in place.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →