What is the string replace() method in Python?

In Python, the replace() method is a built-in function available for string objects. This method allows you to replace occurrences of a specified substring with another substring within the target string. It is a straightforward and efficient way to perform this common string manipulation task.

Syntax of the replace() Method

The method is called on a string object and can be used as follows:

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

Parameters:

  • old: The substring you want to replace.
  • new: The substring that will replace the old substring.
  • count (optional): The maximum number of occurrences to replace. If omitted, all occurrences of the old substring will be replaced.

Returns

The replace() method returns a new string where the specified values are replaced. It does not change the original string; it returns a new string, as strings in Python are immutable.

Examples of Using replace()

Here are several examples illustrating how to use the replace() method:

Basic Usage

text = "Hello World" new_text = text.replace("World", "Universe") print(new_text) # Output: Hello Universe

Replacing Multiple Occurrences

text = "Apples and bananas" new_text = text.replace("a", "o") print(new_text) # Output: Opples ond bononos

Limiting the Number of Replacements

If you only want to replace a limited number of occurrences rather than all, you can use the count parameter:

text = "Apples and bananas" new_text = text.replace("a", "o", 2) print(new_text) # Output: Opples ond bananas

In this example, only the first two instances of "a" are replaced with "o".

Case Sensitive

The replace() method is case sensitive. If you try to replace a substring that doesn't exactly match the case, it won't be replaced:

text = "Hello World" new_text = text.replace("world", "Universe") print(new_text) # Output: Hello World (no change)

To perform a case-insensitive replacement, you might first need to find the substring with the appropriate case or convert the string to a consistent case before replacing:

text = "Hello World" lower_text = text.lower() new_text = lower_text.replace("world", "universe") print(new_text.capitalize()) # Output: Hello universe

Practical Applications

The replace() method is very useful in data cleaning tasks where you need to replace or remove unwanted characters from your data. For example, removing special characters, correcting repeated characters, or replacing abbreviations for full forms in textual data.

Conclusion

The replace() method in Python provides a powerful way to modify strings by replacing parts of the string with new content. Its simplicity and efficiency make it an essential tool for text processing in Python. Remember, since strings are immutable in Python, the replace() method does not alter the original string; instead, it returns a new string with the replacements.

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the acceptance rate for Microsoft final round interviews?
Why are coding interviews hard?
Why are interview skills important?
Which field will be in demand in 2030?
What is the most powerful coding program?
Which language is best for interviews?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.