str.replace()
Returns a copy of a string where every match of one piece of text is swapped for another, or only the first few matches.
str.replace(old, new, /, count=-1)replace() finds every place where the text old appears and puts new there instead. You get back a new string; the original stays as it was.
Use it to fix spelling, remove characters (replace them with ""), change separators, or clean numbers written as text, such as "1.234,50".
It matches exact text and is case-sensitive: replacing "python" does not touch "Python". For patterns such as "any digit" or "any case", use re.sub() from the re module.
Parameters
| Parameter | What it is |
|---|---|
oldrequired | The text to look for. Every match is replaced, unless you set count. |
newrequired | The text to put in its place. Use "" to delete old. |
countoptional, default -1 | The most replacements to make, counting from the left. -1 means all of them. Writing it by name, count=2, works only from Python 3.13; on older versions pass it by position: replace("-", "+", 2). |
Return value
A new string with the replacements made. If old is not found, a copy of the same text.
Examples
1Replace a word
text = "I like cats. Cats like me."
print(text.replace("cats", "dogs"))I like dogs. Cats like me.
"Cats" with a capital C is a different text, so it stays.
2Remove characters
Replace with an empty string to delete something.
phone = "+30 210 123 4567"
print(phone.replace(" ", ""))+302101234567
3Replace only the first matches
path = "a-b-c-d"
print(path.replace("-", "+", 2))a+b+c-d
4Turn a European price into a number
Many countries write a thousand separator with a dot and decimals with a comma. Chain replace() calls to turn it into a form float() understands.
price = "€1.234,50"
clean = price.replace("€", "").replace(".", "").replace(",", ".")
print(clean)
print(float(clean) * 2)1234.50 2469.0
5Fill in a simple template
template = "Dear NAME, your order ships to CITY."
message = template.replace("NAME", "Lena").replace("CITY", "Vienna")
print(message)Dear Lena, your order ships to Vienna.
Common mistakes
Expecting replace() to change the string
Strings never change in place. Assign the result back to the variable.
note = "teh plan"
note.replace("teh", "the")
print(note)
note = note.replace("teh", "the")
print(note)teh plan the plan
Replacing inside other words
replace() does not know about words. Replacing "cat" also changes "category". Use re.sub(r"\bcat\b", …) when you mean whole words only.
text = "cat in the category"
print(text.replace("cat", "dog"))dog in the dogegory
Questions people ask
How do I replace several different characters at once?
Chain calls, text.replace("a", "1").replace("b", "2"), or for single characters use str.translate() with str.maketrans("ab", "12").
How do I replace text ignoring upper and lower case?
Use re.sub("python", "snake", text, flags=re.IGNORECASE) from the re module.
How do I replace only the last match?
Split once from the right and join: new.join(text.rsplit(old, 1)).
Can replace() use a pattern like 'any number'?
No, it only matches exact text. Use re.sub(r"\d+", "#", text) for patterns.