What is the string replace() method in Python?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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
Design Gurus Team

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
Is Tesla a luxury car?
What is the hardest part of a virtual interview?
What is the best place to learn coding for beginners?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.