Can call the string.replace(old, new) method using the string object. This article demonstrates the replace method.
Not all programming languages have a standard string replace function. Python has a lot of functionality that comes out of the box.
Related course: Complete Python Bootcamp: Go from zero to hero in Python
Define a string and call the replace() method. The first parameter is the word to search, the second parameter specifies the new value.
The output needs to be saved in the string. If you don’t save the output, the string variable will contain the same contents. Saving output is done using: s = function()
Try the program below:
s = "Hello World"
An optional parameter is the number of items that will be replaced. By default its all.
The program below replaces only the first item:
s = "Hello World World World"
- Try the replace program