
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to replace all occurrences of a string with another string in Python?
In Python, you can replace all occurrences of a substring within a string using the replace() method. This method returns a new string where every occurrence of the given old substring is replaced with a new substring.
To match the string with case-insensitivity, you need to use regular expressions.
Using the replace() Method
The Python replace() method returns a new string where all occurrences of the old substring are replaced with the new substring. It does not modify the original string because strings in Python are immutable.
string.replace(oldvalue, newvalue, count)
where,
-
Old value: The substring that you want to replace.
-
New value: This represents the substring with which you want to replace.
-
Count: This is an optional parameter; it is used to specify the number of old values you want to replace with new values.
Example
In the following example, we replace all occurrences of the substring "apple" with "orange" in the given string -
text = "I like apple. Apple is my favorite fruit. apple pie is delicious." new_text = text.replace("apple", "orange") print(new_text)
Following is the output obtained -
I like orange. Apple is my favorite fruit. orange pie is delicious.
The replace() method is case-sensitive. In the example above, the word "Apple" with a capital 'A' was not replaced.
Replacing with Case-Insensitivity
If you want to replace all occurrences of a string regardless of case, you can use the re.sub() function.
This function accepts three string values representing the original string, the string to replace it with, and the text as parameters, respectively, and substitutes/replaces the original string with the new string.
This function accepts an optional parameter flag. If we pass re.IGNORECASE as a flag in this method replaces the string, ignoring the case (case-insensitively).
Example
In this example, we use re.sub() function with the re.IGNORECASE
flag to replace all case variations of "apple" -
import re text = "I like apple. Apple is my favorite fruit. apple pie is delicious." new_text = re.sub(r"apple", "orange", text, flags=re.IGNORECASE) print(new_text)
We get the output as shown below -
I like orange. orange is my favorite fruit. orange pie is delicious.
Replacing Only a Limited Number of Occurrences
You can also specify the maximum number of replacements by passing a third argument, count, to the replace() function.
Example
In this example, only the first two occurrences of "apple" are replaced with "orange" -
text = "apple apple apple apple" new_text = text.replace("apple", "orange", 2) print(new_text)
The output obtained is as shown below -
orange orange apple apple