Python program to Replace a Substring of a String

str = "Hello, World, Table, Chair, Cup, Tea"
print(str)
substr1 = input("Choose an old substring to replace: ")
substr2 = input("Insert new substring: ")
lensubstr1 = len(substr1)
while str.find(substr1) > 0:
 i = str.find(substr1)
 str = str[:i] + substr2 + str[i+lensubstr1:]
print(str)

 

 

Output:

Hello, World, Table, Chair, Cup, Tea
Choose an old substring to replace: Chair
Insert new substring: parrot
Hello, World, Table, parrot, Cup, Tea

Subscribe For Daily Updates