
Author
Abhinav Rawat
Product Manager @ Interview Kickstart | Ex-upGrad | BITS Pilani. Working with hiring managers from top companies like Meta, Apple, Google, Amazon etc to build structured interview process BootCamps across domains
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
Time Zone:
Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Just drop your name and email so we can send your Power Patterns PDF straight to your inbox. No Spam!
By sharing your contact details, you agree to our privacy policy.
Time Zone: Asia/Dhaka
We’ve sent the Power Patterns PDF to your inbox — it should arrive in the next 30 seconds.
📩 Can’t find it? Check your promotions or spam folder — and mark us as safe so you don’t miss future insights.
We’re hosting a private session where FAANG insiders walk through how they actually use these Power Patterns to crack interviews — and what sets top performers apart.
🎯 If you liked the PDF, you’ll love what we’re sharing next.
Time Zone: