Python 3 – String replace() Method
In Python, the replace()
method is used to replace all occurrences of a particular substring in a given string with another substring. It returns a new string with the specified replacements.
Syntax
The syntax for using the replace()
method is as follows:
string.replace(old, new[, count])
Here, old
represents the substring in the original string that needs to be replaced with new
. The count
parameter is optional and represents the total number of replacements you want to make. If you do not specify the count
parameter, then all occurrences of old
in the string will be replaced by new
.
Example
Let us look at a simple example to understand the working of the replace()
method:
string = "Hello World"
new_string = string.replace("World", "Universe")
print(new_string) # Hello Universe
In the above example, we first create a string called string
with the value "Hello World"
. Then we call the replace()
method on this string and specify "World"
as the substring that we want to replace with "Universe"
. The resulting string is "Hello Universe"
, and we store it in a new variable called new_string
. Finally, we print the value of new_string
using the print()
function.
Using the count parameter
Now let us look at an example where we use the optional count
parameter:
string = "Hello World, World, World"
new_string = string.replace("World", "Universe", 2)
print(new_string) # Hello Universe, Universe, World
In this example, we have added the count
parameter to the replace()
method and set it to 2
. This means that only the first two occurrences of "World"
will be replaced with "Universe"
. The resulting string is "Hello Universe, Universe, World"
, and we print it to the console using the print()
function.
Replacing multiple substrings
It is also possible to use the replace()
method to replace multiple substrings in a single string. Let us look at an example:
string = "1, 2, 3, 4, 5"
new_string = string.replace("1", "A").replace("2", "B").replace("3", "C").replace("4", "D").replace("5", "E")
print(new_string) # A, B, C, D, E
In this example, we create a string called string
with the value "1, 2, 3, 4, 5"
. Then we chain multiple calls to the replace()
method to replace each number with a corresponding letter. The resulting string is "A, B, C, D, E"
, and we print it to the console using the print()
function.
Replacing with a lambda function
In addition to regular strings, it is also possible to use a lambda function to replace substrings in a string. Here is an example:
string = "1, 2, 3, 4, 5"
new_string = re.sub(r'\d+', lambda m: str(int(m.group(0)) * 2), string)
print(new_string) # 2, 4, 6, 8, 10
In this example, we first import the re
module (which stands for “Regular Expressions”) to use its sub()
method, which allows us to replace substrings in a string using a lambda function. We define a lambda function that takes the matched substring (m
) and multiplies it by two. The re.sub()
method then uses this lambda function to replace all occurrences of digits in the string with their doubled values. The resulting string is "2, 4, 6, 8, 10"
, and we print it to the console using the print()
function.
Conclusion
In this article, we have looked at the replace()
method in Python and learned how to use it to replace substrings in a string. We have seen how to use the count
parameter to limit the number of replacements, how to chain multiple replace()
calls to replace multiple substrings, and how to use a lambda function with the re.sub()
method to replace substrings using regular expressions. Now that you have learned about the replace()
method, you can start using it in your own Python projects to manipulate strings with ease.