home
NOTE: Under Construction - I'm in the middle of upgrading my site and lots of stuff is kinda broken. Please forgive the mess.

Replace A String With .replace() In Python

January 2022

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

Code
example = "the quick brown fox is quick"

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

print(update)
Results
the slow brown fox is slow

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

Code
example = 'alfa alfa alfa alfa'

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

print(update)
Results
bravo bravo alfa alfa
═══ § ═══