[Easy Python] replace multiple characters

Use the replace method in a string object.

Replace method gets three params: two mandatory (the first string what we want to replace and the second string with which we replace) and one optional (count of replacement).

my_string = "Hello world!"
print(my_string.replace("Hello", "Goodbye"))

my_string = "banana, banana, orange"
print(my_string.replace("banana", "apple"))

print(my_string.replace("banana", "apple", 1))
Roman Ivashchenko profile picture
Dec 09, 2020
by Roman Ivashchenko
Did it help you?
Yes !

Best related