Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › Python › Reference › replace()

Python Reference

str.replace()

Replace all occurrences of a substring with another string.

Last updated: October 2026

Definition: str.replace(old, new, count=-1) returns a copy of the string with all occurrences of the substring old replaced by new. It does not modify the original string.

🎬 Live Example

Five common replace patterns. Click any button to see the output.

Input:

                
Output:

            

💡 replace returns a new string — strings are immutable.

📝

Basic Syntax

str.replace(old, new, count=-1)

# old   — substring to find
# new   — replacement string
# count — max replacements (default -1 = all)
# Returns: new str

🎯 Parameters

Parameter Type Description
oldstrSubstring to replace.
newstrReplacement string.
countintMax replacements. -1 = all (default).
1

Basic Replacement

"hello world".replace("world", "Python")
# 'hello Python'

"aaa".replace("a", "b")
# 'bbb'

"cat".replace("c", "b")
# 'bat'
2

Limit Replacements with count

"aaa".replace("a", "b", 1)
# 'baa'

"aaa".replace("a", "b", 2)
# 'bba'

"hello hello".replace("hello", "hi", 1)
# 'hi hello'

Use case: Replace only the first occurrence of a delimiter when parsing.

3

Remove Substrings

"hello world".replace(" ", "")
# 'helloworld'

"a-b-c".replace("-", "")
# 'abc'

# Strip specific characters anywhere
"phone: 123-456".replace("-", "")
# 'phone: 123456'
4

Case Sensitivity

"Hello".replace("hello", "Hi")
# 'Hello'  (no match — case matters)

"Hello".lower().replace("hello", "hi")
# 'hi'

# Or use regex for case-insensitive
import re
re.sub("hello", "hi", "Hello", flags=re.IGNORECASE)
# 'hi'
5

Real-World: Clean User Input

raw = "  Hello,   World!  "

cleaned = raw.strip().replace("  ", " ")
# 'Hello, World!'

# Remove multiple spaces
import re
re.sub(r"\\s+", " ", raw.strip())
# 'Hello, World!'

🎯 replace vs translate vs re.sub

Method Use when Notes
replaceSimple literal replacementCase-sensitive, no regex
translateCharacter-by-character mappingFast, single chars
re.subPattern matchingRequires regex

⚠️ Common Mistakes

❓ Frequently Asked Questions

What does replace() do in Python?

str.replace(old, new) returns a copy of the string with all occurrences of the substring old replaced by new. It does not modify the original string.

How do I limit the number of replacements?

Pass count as the third argument: 'aaa'.replace('a', 'b', 2) returns 'bba'. Only the first 2 occurrences are replaced.

Is replace case-sensitive?

Yes. replace is case-sensitive. 'Hello'.replace('hello', 'Hi') returns 'Hello' unchanged. Use lower() or regex for case-insensitive replacement.

Does replace work with regex?

No. str.replace does literal substring replacement. For regex, use the re module: re.sub(pattern, replacement, string).

Does replace modify the original string?

No. Strings are immutable in Python. replace returns a new string; the original is unchanged.

🎯 What's Next?

← All Python Reference