home ~ projects ~ socials

Replace A String With .replace() In Python

Use .replace() to do basic text replacement in a string. For example:

example = "the quick brown fox is quick"

update = example.replace("quick", "slow")

print(update)
Output:
the slow brown fox is slow

If you only want to replace a certain number, add a third parameter with the count.

example = 'alfa alfa alfa alfa'

update = example.replace('alfa', 'bravo', 2)

print(update)
Output:
bravo bravo alfa alfa
-- end of line --