str.replace() / str.translate()
Methods for replacing specific characters or substrings within a string with another string. You can also perform fast bulk replacement using a translation table.
Syntax
# Replaces all occurrences of old with new in the string (returns a new string). str.replace(old, new, count=-1) # Creates a translation table for multi-character substitution. str.maketrans(x, y=None, z=None) # Replaces characters using a translation table created with maketrans(). str.translate(table)
Method List
| Method | Description |
|---|---|
| str.replace(old, new, count=-1) | Replaces occurrences of old with new in the string. If count is specified, at most count replacements are made from the left. The original string is not modified. |
| str.maketrans(x, y, z) | Creates a translation table (dictionary) for use with translate(). Passing x and y creates a one-to-one character mapping; passing z maps each character in that string to None (deletion). |
| str.translate(table) | Translates or deletes characters based on the table created by maketrans(). |
Sample Code
# Replace a substring using replace().
text = "Python is hard. Let's learn Python. Python is wonderful."
result = text.replace("hard", "fun")
print(result) # Python is fun. Let's learn Python. Python is wonderful.
# Limit the number of replacements with count.
text2 = "a b c a b c a"
print(text2.replace("a", "X", 2)) # X b c X b c a (only the first 2 are replaced)
# To delete characters, replace them with an empty string.
html_like = "Hello, <b>Alice</b>!"
clean = html_like.replace("<b>", "").replace("</b>", "")
print(clean) # Hello, Alice!
# replace() does not modify the original string (strings are immutable).
original = "hello"
new = original.replace("l", "r")
print(original) # hello (unchanged)
print(new) # herro
# Use maketrans() and translate() to convert multiple characters at once.
# A simple example converting lowercase vowels to uppercase.
table = str.maketrans("aeiou", "AEIOU")
print("hello world".translate(table)) # hEllO wOrld
# Convert digits to their fullwidth equivalents.
digit_table = str.maketrans("0123456789", "0123456789")
print("2026-03-05".translate(digit_table)) # 2026-03-05
# Delete specific characters (characters in the third argument are removed).
remove_table = str.maketrans("", "", "aeiou") # Remove vowels.
print("hello world".translate(remove_table)) # hll wrld
# You can also specify a translation table using a dictionary (with Unicode code points).
table2 = str.maketrans({"A": "1", "B": "2", "C": "3"})
print("ABCABC".translate(table2)) # 123123
# A practical example: normalize mixed line endings.
mixed_newlines = "line1\r\nline2\rline3\nline4"
unified = mixed_newlines.replace("\r\n", "\n").replace("\r", "\n")
print(repr(unified)) # Normalized to '\n' line endings.
Notes
Python strings are immutable (cannot be changed in place). replace() does not modify the original string — it returns a new string object with the replacements applied. To update the original variable, reassign it: text = text.replace(old, new).
When replacing multiple substrings in sequence, chaining replace() calls (method chaining) is the common approach. However, when the number of replacements is large, translate() is faster. For text cleansing — bulk deletion or conversion of unwanted characters — translate() is particularly effective.
For advanced pattern matching and replacement using regular expressions, use re.sub() from the standard library's re module. For example, to collapse consecutive whitespace into a single space, use re.sub(r"\s+", " ", text). For string searching, see str.find() / str.index().
If you find any errors or copyright issues, please contact us.