Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Python String Replace() Method

Last updated by Abhinav Rawat on Apr 01, 2024 at 01:48 PM | Reading time: 5 minutes

The fast well prepared banner

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Python String Replace() Method
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings

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


Last updated on: 
April 1, 2024
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

Attend our Free Webinar on How to Nail Your Next Technical Interview

Register for our webinar

How to Nail your next Technical Interview

1
Enter details
2
Select webinar slot
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

Python String Replace() Method

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Our tried & tested strategy for cracking interviews
blue tick
How FAANG hiring process works
blue tick
The 4 areas you must prepare for
blue tick
How you can accelerate your learnings
Register for Webinar
entroll-image