python logo

python string replace


Python hosting: Host, run, and code Python in the cloud!

The method replace returns a new string with old replaced by new.

Syntax


The format is:

str.replace(old, new, max)


Parameters

Example

Parameter
old The string to be replaced
new The string to be used
max Maximum amount of replacements.Leave blank for infinite.
s = "All work and no play makes Jack a dull boy. Jack?"
s = s.replace("Jack","Joe")
print(s)

Result:

All work and no play makes Joe a dull boy. Joe?





Leave a Reply: