Google News
logo
Python Program to Replace a string with another string
In the following example of Python program that demonstrates how to replace a substring in a string with another substring using the built-in replace() function :
Program :
# Replace a string with another string
my_string = "Hello, World!"
new_string = my_string.replace("World", "Python")

print("The new string is:", new_string)
Output :
The new string is: Hello, Python!
In the above program, we start by assigning the string value `"Hello, World!"` to the variable `my_string`. We then use the `replace()` function to replace the substring `"World"` in `my_string` with the substring `"Python"`, and assign the result to the variable `new_string`.

Finally, we use the `print()` function to print the message "The new string is:" followed by the value of `new_string`.

As you can see, the `replace()` function replaces all occurrences of the substring `"World"` in the string with the substring `"Python"`.