How to Use String replace() Function

You can easily use the replace() function, you just need to call the function with a string object and pass the strings as a parameter. The first parameter is the substring you want to replace, and the second parameter is the string you want to replace with.

Let’s understand it better how to replace a string in Python with a simple example:

Python3




string = "Replace"
new_string = string.replace("Replace", "Replaced")
 
print(new_string)


Output

Replaced

Python String replace() Method

The string replace() method returns a copy of the string where occurrences of a substring are replaced with another substring.

Example:

Python3




string = "Hello World"
new_string = string.replace("Hello", "Good Bye")
 
print(new_string)


Output

Good Bye World

Similar Reads

What is String replace() Method?

...

String replace() Method Syntax

String replace() is a built-in function in Python and it is used to replace a substring with another string. It will replace every occurrence of that substring, so it should be used with caution....

How to Use String replace() Function

string.replace(old, new, count)...

More Examples of String replace() Method

You can easily use the replace() function, you just need to call the function with a string object and pass the strings as a parameter. The first parameter is the substring you want to replace, and the second parameter is the string you want to replace with....