August 17, 2024
Python string replacement with replace() function
You can replace a character, a word or even a string from a string through replace function in Python. replace function required two string parameters to make replacement on the string first parameter old string which you want to replace and second parameter is new string with you want to replace old string. We need to call the replace function on a string object The example of replace method is mentioned below:
Code
greetingStr = "Good Morning";
print(greetingStr)
result = greetingStr.replace("Morning", "Evening")
print(result)
In above mentioned example we have a string variable with the name of greetingStr and we make a replacement with replace method, we replaced 'Morning' with 'Evening'.