Python Tutorial

String replace

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

Syntax

The format is:
str.replace(old, new, max)

Parameters

Example

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?

Parameter
old The string to be replaced
new The string to be used
max Maximum amount of replacements.Leave blank for infinite.