Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Replace A String With .replace() In Python

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