About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Python String Replace() Method

In most software engineering interview problems, string handling can become a tedious task. Both Python and Java language provide some well-defined library functions that can make working with strings easy. Python’s string replace() method is one such built-in function, and we will cover the details of it in this article:

  • What is the replace() method in Python?
  • replace() without count parameter
  • replace() with count parameter
  • replace() with user input
  • replace() with escape sequences
  • FAQs on the replace() method in Python

What Is the replace() Method in Python?

Python’s string replace() method is a built-in function that returns a copy of a string, where all occurrences of a substring are replaced with another substring. 

Syntax of the replace() method:

Parameters of the replace() method:

  1. old: Original string that needs to be replaced.
  2. new: New string that will replace the old string.
  3. count: The number of times replacing should be done.

count is an optional parameter; if not specified, all the occurrences of the old substring are replaced with the new substring. 

The replace() method returns a copy of the string where the old substring is replaced with the new substring. The original string is unchanged. If the old substring is not found, it returns the copy of the original string.

Example 1: replace() Method Without Count Parameter

Let’s create a file named “replace.py” and assign a variable ”input_string” to the string mentioned below:

input_string = "Thor was more powerful.Thor was the most popular of all the gods. Thor was a god of war and lightning."
print(input_string.replace("Thor","Loki"))

Output: “Loki was more powerful.Loki was the most popular of all the gods. Loki was a god of war and lightning.”

Note: In this example, we have not used the count parameter. If count is not specified, all the occurrences of the old substring are replaced with the new substring. In this case, three occurrences of the word “Thor” are replaced by ”Loki.”

Example 2: replace() With Count Parameter

Let’s create a file named “replaceWithCount.py” and assign a variable ”input_string” to the string mentioned below:

input_string = "Thor was the most popular of all the gods. Thor was a god of war and lightning. Thor was physically strong."
print(input_string.replace("Thor","Loki",2))

In this example, we use the count parameter. The input string contains three occurrences of “Thor.” We only want to change the first two occurrences of it. So, we pass 2 as the value of the count parameter.

Output: “Loki was the most popular of all the gods. Loki was a god of war and lightning. Thor was physically strong.”

Note: If we pass count value as 0, there will be no change in the original string.

input_string = "Thor was the most popular of all the gods. Thor was a god of war and lightning. Thor was physically strong."
print(input_string.replace("Thor","Loki",0))

Output: “Thor was the most popular of all the gods. Thor was a god of war and lightning. Thor was physically strong.”

Example 3: replace() With User Input

In this example, we save a string in a file. Then we read the file and replace all occurrences of “Thor” with “Loki.”

input_string = input()

delimiter = "Thor"

new_delimiter = "Loki"

modified_string = input_string.replace(delimiter , new_delimiter)

print("modified_string =", modified_string)

Example 4: replace() With Escape Sequence

Following are some escape characters and how you can use them in code:

Replacing “/” with “//”

input_string = "This is backslash /"
print(input_string.replace("/","//"))

Output: This is backslash //

Replacing space with “ (double quotation) 

input_string= "here's doublequote"
print(input_string.replace(" ", "\""))

Output: here’s”doublequote

FAQs on the replace() Method in Python

Question 1: If Strings are immutable, how does replace() method work?

Answer: String replace does not modify the original string; rather, it creates a copy of the original string.

input_string = "Thor was more powerful.Thor was the most popular of all the gods. Thor was a god of war and lightning."
new_string = (input_string.replace("was","is"))

print(input_string)

print(new_string)

Output:

Thor was more powerful.Thor was the most popular of all the gods. Thor was a god of war and lightning.

Thor is more powerful.Thor is the most popular of all the gods. Thor is a god of war and lightning.

We changed the string and stored it in a variable named new_string. In the output, print(input_string) printed the original string, proving that it is not modified.

Question 2: Can we replace a single alphabet using the replace() method?

Answer: Yes, we can replace a single alphabet using replace(). Suppose we have an input_string = “Thor was a god of car and lightning,” and we want to replace the c in “car” with w. We can use new_string = input_string.replace(“c”,  “w”) in our code to get the desired result: “ Thor was a god of war and lightning.”

Preparing for a Tech Interview? 

If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the toughest coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

---------

Article contributed by Problem Setters Official