What is String replace() Method?

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.

It does not change the original string but returns a new one. It is mostly used in string substitution.

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....