How to use Python Replace function
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.
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
Best-suited Python courses for you
Learn Python with these high-rated online courses
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)
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.
Examples of replace () function in Python
Example -1 (i): Replacing all instances of a single character
#replace 'e' with 'E' in Infoedgename = "Infoedge"new_name = name.replace('e', 'E')new_name
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
Output
Example – 2: Replace all the characters of a string
#replace all the characters of a stringname = "Hi! my name is InfoEdge"new_name = name.replace ('InfoEdge', 'Naukri')new_name
Output
![Top 110+ Python Interview Questions and Answers](https://images.shiksha.com/mediadata/ugcDocuments/images/wordpressImages/2021_07_Python-Interview-Questions_b.jpg)
![Pandas Interview Questions for Data Scientists](https://images.shiksha.com/mediadata/ugcDocuments/images/wordpressImages/2022_04_pandas_b.jpg)
![Top 9 NumPy Interview Questions](https://images.shiksha.com/mediadata/ugcDocuments/images/wordpressImages/2022_04_NumPy-Interview-Questions_b.jpg)
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
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
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 requote = "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)
Output
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 |