Python

Learn about replace() method in Python

Learn about replace() method in Python

Introduction

We'll learn about the replace() method in Python and how to utilize it in this tutorial. Python's built-in replace() method swaps out one substring for another in any number of instances where it appears in the original string. When necessary, it replaces occurrences of the old substring in the returned copy of the initial string with the new substring that was wanted.

Signature

replace(old, new[, count])

Return a duplicate of the string where the old substring has been changed to the new substring. Only the first count instances are replaced if the optional parameter count is provided.

Parameters

  1. old : an outdated string that will be changed.
  2. new : The old string will be replaced by a new string.
  3. count : how many times to do the replacement.

Return

It gives you a string. Let's look at some examples to better understand the capabilities of the replace() method.

Code Example

str = "Java is a programming language" 
str2 = str.replace("Java","C"
print("Old String: \n",str) 
print("New String: \n",str2) 

Output:
Old String:
Java is a programming language
New String:
C is a programming language

write your code here: Coding Playground

Code Example

str = "Java C Java Php C# Python Java " 
str2 = str.replace("Java","C#") # replaces all the occurrences 
print("Old String: \n",str) 
print("New String: \n",str2) 
str2 = str.replace("Java","C#",1) # replaces first occurrences only 
print("\n Old String: \n",str) 
print("New String: \n",str2)  

write your code here: Coding Playground

Output

Old String: 
Java C Java Php C# Python Java
New String:
C# C C# C# Php Python C#


Old String

Java C Java Php C# Python Java
New String:
C# C C# Java Php Python Java

Code Example

# Python replace() method example 
# Variable declaration 
str = "Apple is a fruit" 
# calling function 
str2 = str.replace(str,"Tomato is also a fruit"
# displaying result 
print(str2) 
Output:
Tomato is also a fruit

write your code here: Coding Playground

Common replace() function FAQs

Q1. What does Python's replace() function do?

->The replace() method in Python produces a duplicate of the original string that, where necessary, replaces all occurrences of the old substring with the new desired substring.

Q2. What is the number of replacements argument's default value in the replace() function?

-> Python replace() function by default replaces all occurrences of the old substring with the desired substring.

Q3. In Python, how do you replace two characters in a string?

-> In Python, we may replace numerous characters in a string using the replace(), subn(), sub(), maketrans(), and translate() methods.

Q4. What parameters are needed for replace()?

-> The old substring that needs to be replaced and the new substring that needs to replace all occurrences of the old substring are both required parameters for the replace() function.

Q5. In Python, how do we replace a text in a list?

-> With the help of a for loop and the replace() method in Python, we may replace a text in a list.