String Methods

str.replace()

Replace non-overlapping occurrences of a literal substring from left to right. An omitted or negative count permits every match; zero performs no replacement. An empty old string matches insertion boundaries. Replacement text is not searched again during the operation. This is not regular-expression substitution. The original string is unchanged.

Formal Syntax & Structure

python syntaxpython
text.replace(old, new, /, count=-1)

Parameters & Attributes

NameTypeDescription
oldstrLiteral substring to replace.
newstrReplacement string, possibly empty.
countintMaximum replacements; default -1 means unlimited. Keyword accepted in 3.13.

Return Value

str

Text after the requested substitutions.

Working Example

Example Snippetpython
label = "draft-draft-draft"print(label.replace("draft", "ready", count=2))print("۱۲-۳۴".replace("-", ""))print(label)
Python Reference