Python string Method - replace()

The replace() method is a built-in string method in Python that returns a new string with all occurrences of a specified substring replaced with another substring.

The syntax for using replace() method is as follows:

re‮ot ref‬:theitroad.com
string.replace(old, new, count)

Here, string is the original string, old is the substring to be replaced, new is the substring to replace old with, and count is an optional parameter that specifies the maximum number of occurrences of old to be replaced. If count is not specified, all occurrences of old are replaced.

The method returns a new string with all occurrences of old replaced by new.

Here's an example of using the replace() method:

string = "Hello, World!"
new_string = string.replace("World", "Python")
print(new_string)

Output:

Hello, Python!

In the example above, the replace() method was used to replace the substring "World" in the original string with "Python". The resulting string is stored in the variable new_string.