How to use Python Replace function

How to use Python Replace function

3 mins read1.8K Views Comment
Updated on Oct 26, 2023 17:28 IST

Python replace function is used to replace the old string with the new string. In this article, we will briefly discuss how to use python replace function with the help of examples.

2022_11_MicrosoftTeams-image-79.jpg

While updating your program or set of codes, you find that you need to input the right keyword correctly or you plan to change the existing keyword or phrase. Then, how will you do that?
It seems easy if the keyword or the phase is not repeated, but what if it is repeated multiple times?
You can’t search line-by-line code or a string to replace it one by one. Python offers a built-in function, replace (), to resolve the above issue.
So, let’s dive deep to learn more about using replace () function in Python.

Programming Online Courses and Certification Python Online Courses and Certifications
Data Science Online Courses and Certifications Machine Learning Online Courses and Certifications

Table of Content

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

What is replace () function in Python?

replace () function in Python used to replace the old string (or old value) with the new string (or new value)

  • replace () function is case-sensitive
  • It returns a copy of a string, where the new value replaces the old value, but the original string remains the same, i.e., unchanged.
  • If the mentioned old string is not found, it will return the original string.

Syntax


 
str.replace (old, new, count)
Copy code

Parameters

  • old: The string you want to replace.
  • new: The string which will replace the old string
  • count: 
    • It is optional.
    • It indicates how many occurrences of the old string will be replaced by the new string.
    • If the count is not mentioned, it will replace all the occurrences of the old string with the new string.
      • Only the first count occurrences will be replaced if the count is given.
What is Programming What is Python
What is Data Science What is Machine Learning

Examples of replace () function in Python

Example -1 (i): Replacing all instances of a single character


 
#replace 'e' with 'E' in Infoedge
name = "Infoedge"
new_name = name.replace('e', 'E')
new_name
Copy code

Output

Example – 1 (ii) – replace is case sensitive


 
#replace 'e' with 'E' in Infoedge
#replace is a case-sensitive, i.e., if in this case, we put old_value = E and new_value = e,
#then replace () function will return the original string
name = "Infoedge"
new_name = name.replace ('E', 'e')
new_name
Copy code

Output

Example – 2: Replace all the characters of a string


 
#replace all the characters of a string
name = "Hi! my name is InfoEdge"
new_name = name.replace ('InfoEdge', 'Naukri')
new_name
Copy code

Output

Top 110+ Python Interview Questions and Answers
Pandas Interview Questions for Data Scientists
Top 9 NumPy Interview Questions

Example – 3: Use of count parameter – replace () a certain number of occurrences in the string


 
#use count parameter in replace() function
#replace the first 2 occurrences of character 'h' with H
quote = "No heart is so hard as the timid heart."
new_quote = quote.replace('h', 'H', 2)
new_quote
Copy code

Output

Note: In the above example, if we do not use count = 2, it will replace all the ‘h’ with ‘H.’ let’s have a look at the above example without count = 2.


 
quote = "No heart is so hard as the timid heart."
new_quote = quote.replace('h', 'H')
new_quote
Copy code

Output

If you are following the article very closely, you will notice that we are replacing the perfect matching substring, i.e., if we have to replace ‘P’ in Python with ‘S’, then we have to use replace(‘P’, ‘S’); otherwise, it will return the old string.
But what if we have the string like:
“Python is the most used programming language by Programmers around the globe,”
And do we have to replace all the P’s (P and p both) here with P?
Then how will you do that?

Let’s see how to replace case-sensitive strings.

Python regex Replace Case Insensitive Substring Substitutions


 
#Case-Insensitive Substring Substitution in Python
#replace JAVA, Java, and java with Python
import re
quote = "JAVA is the most used programming language. I'm learning Java as it is easy to learn java."
quote = re.sub("Java", "Python", quote, flags=re.IGNORECASE)
print(quote)
Copy code

Output

2022_11_image-61.jpg

Conclusion

str.replace (old, new, count) is a built-in python function that replaces a character/substring of a string. replace () function is case-sensitive, i.e., it replaces a perfectly matching substring, and it will return the old string if the given substring does not find in the string.
In this article, we have also discussed how to replace case-insensitive substrings.
Hope this article helps you to learn how to use replace function in Python.

Top Trending Python Article

Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python

Python Interview Questions

Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |

About the Author