What is replace method and how to use it?
- The Python string replace method is used to “replace” the new substring with the existing substring in the specified string.
- The replace method returns a copy of the string with replaced substring(s).
- The source string remains unchanged after using the replace method.
- Replace function is case-sensitive.
- See the last example to learn how to perform case-insensitive replacements.
Syntax for using replace method
This is how you may use the replace method with its parameters:
Where:
Replace Method Parameters | Description |
source string | The source string is the one you want to perform the replacement in. |
old_substring | The existing substring in the source string you want to replace. |
new_substring | The old_substring will be replaced by new_substring. |
Count |
|
An example of using Python replace method
In this example, a simple string is created. Another string variable is used which value is the returned value after using the replace method in the first string.
The source string is:
“A tutorial for learning Python replace function”
The word replace will be changed to “String replace”, as shown in the example below:
The code:
src_str = "A tutorial for learning Python replace function" str_replaced = src_str.replace("replace","String replace") print ("String after replace method:", str_replaced)
Output:
What if more than one match is found?
In this example, the following string is created:
By using the Python replace method, I will change “This” to “that” without specifying the count parameter. See what happens..
The code:
#Python Strings replace method demo src_str = "This is a test sentence with titlecase. This is a test sentence with titlecase. This is a test sentence with titlecase." str_replaced = src_str.replace("This","that") print (str_replaced)
Output:
In the output, you can see the word “this” is replaced in all occurrences to “that” i.e. three times. You may use the count parameter to limit the replacements. See next example.
An example of using the count parameter of replace method
Using the same string as in above example with the count parameter with value = 2. See the code and output:
#Python Strings replace method demo src_str = "This is a test sentence with titlecase. This is a test sentence with titlecase. This is a test sentence with titlecase." str_replaced = src_str.replace("This","that",2) print (str_replaced)
Output:
You can see that “this” is replaced only two times. The replacement is started from the left or only the first matches are changed.
I want to replace the first occurrence from right, now what?
Unfortunately, the Python replace function will change from left to right only and there is no straightforward thing like “replace” etc. However, where there is a will there is a way.
The trick is using the join and rsplit methods together to get the result. In that case, specify the term to be changed in the join method and the existing term in the rsplit method as shown in the example below:
#A demo of replacing from right str_rep_right = "This is Python. This is replace method." print('That'.join(str_rep_right.rsplit('This', 1)))
Output:
In the output, you can see “This” is changed to “That” from the right side. I used the 1 in the rsplit method, so it changed only one occurrence from the right side. You may question the performance of using both methods, however, this can be the way out.
Learn more about the Python join and rsplit methods.
Is the String replace function case-sensitive?
Yes, the replace function is case-sensitive. That means, the word “this” has a different meaning to “This” or “THIS”.
In the following example, a string is created with different case letters, which is followed by using the Python replace string method.
The code:
#Python Strings replace method demo src_str = "This is a test sentence. this is a test sentence. THIS is a test sentence." str_replaced = src_str.replace("this","that") print (str_replaced)
Result:
You can see, the source string contains “this”, “This” and “THIS”. In the replace string method, the replacement term is:
So, only the second occurrence is changed.
Need to perform case-insensitive replacements?
The Python string replace method does not support case-insensitive replacement. In order to perform a case-insensitive replacement, you may use the regular expression.
The re.IGNORECASE can do this job as shown in an example below.
The code:
import re src_str = re.compile("this", re.IGNORECASE) str_replaced = src_str.sub("that", "This is a test sentence. this is a test sentence. THIS is a test sentence.") print (str_replaced)
Output:
You can see that the same string is used as in the above replace method’s example. The regular expression’s sub method is used with the re.IGNORECASE.
In the output, you can see all occurrences of “this”, irrespective of the case are changed to “that”.