Interview Kickstart has enabled over 21000 engineers to uplevel.
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:
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:
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.
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.”
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.”
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)
Following are some escape characters and how you can use them in code:
input_string = "This is backslash /"
print(input_string.replace("/","//"))
Output: This is backslash //
input_string= "here's doublequote"
print(input_string.replace(" ", "\""))
Output: here’s”doublequote
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.”
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!
---------
Article contributed by Problem Setters Official
Attend our webinar on
"How to nail your next tech interview" and learn