Python String Replace() Method

Last updated by Abhinav Rawat on Sep 25, 2024 at 11:00 PM
Contents

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

Last updated on: September 25, 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
Register for our webinar

Uplevel your career with AI/ML/GenAI

Loading_icon
Loading...
1 Enter details
2 Select webinar slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Spring vs Spring Boot

Spring vs Spring Boot: Simplifying Java Application Development

Reinforcement Learning: Teaching Machines to Make Optimal Decisions

Reinforcement Learning: Teaching Machines to Make Optimal Decisions

Product Marketing vs Product Management

Product Marketing vs Product Management

Java Scanner reset()

The upper() Function in Python

Insertion Sort Algorithm

Ready to Enroll?

Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC

Register for our webinar

How to Nail your next Technical Interview

Loading_icon
Loading...
1 Enter details
2 Select slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Get tech interview-ready to navigate a tough job market

Best suitable for: Software Professionals with 5+ years of exprerience
Register for our FREE Webinar

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC