How to Remove Characters from a String in Python

To remove a character from a string in Python, use replace() with an empty string: text.replace("-", "") removes every hyphen. To remove several different characters at once, use text.translate(str.maketrans("", "", "-()")) or re.sub(r"[-()]", "", text). Strings are immutable, so each method returns a new string that you must assign. This guide covers removing a character by value or by index, multiple characters, strip() and why it removes more than you expect, substrings and whole words, digits and symbols, lists and pandas columns, and which method is fastest.

All examples were run with Python 3.12.5 and pandas 3.0.6 in the Windows Command Prompt. Reference: Python string methods and the re.sub() documentation.

Remove a character with replace()

replace(old, "") removes every occurrence; the optional third argument limits how many are removed:

phone = "(555) 123-4567"

digits_only = phone.replace("-", "").replace("(", "").replace(")", "").replace(" ", "")
print(digits_only)

print(phone.replace("-", ""))        # remove every "-"
print(phone.replace("5", "", 1))     # remove only the first "5"

Output:

5551234567
(555) 1234567
(55) 123-4567
Command Prompt output removing characters from a phone number string in Python with chained replace calls and a count argument
Removing characters with replace().

Remove a character at a specific index

Strings cannot be changed in place, so build a new one from the parts before and after the index:

word = "Pythonn"

print(word[:6] + word[7:])    # remove the character at index 6
print(word[1:])               # remove the first character
print(word[:-1])              # remove the last character

i = word.find("h")            # remove the first "h", wherever it is
print(word[:i] + word[i + 1:] if i != -1 else word)

Output:

Python
ythonn
Python
Pytonn

Remove multiple characters from a string

Chaining replace() gets long quickly. These four approaches all remove $ , ( ) ! in one step:

import re

text = "Price: $1,299.99 (incl. tax)!"
remove = "$,()!"

# 1. translate: one pass, fastest for single characters
print(text.translate(str.maketrans("", "", remove)))

# 2. regular expression with a character class
print(re.sub(r"[$,()!]", "", text))

# 3. loop over the characters with replace
cleaned = text
for ch in remove:
    cleaned = cleaned.replace(ch, "")
print(cleaned)

# 4. keep only the characters you want
print("".join(ch for ch in text if ch not in remove))

Output:

Price: 1299.99 incl. tax
Price: 1299.99 incl. tax
Price: 1299.99 incl. tax
Price: 1299.99 incl. tax
Command Prompt showing four Python methods removing the characters $ , ( ) and ! from a price string with the same result
translate(), re.sub(), a replace() loop and join() give the same result.

To remove all punctuation and symbols, see remove special characters except spaces. To replace characters with something else instead of removing them, see replace multiple characters in a string.

strip() with multiple characters (and removeprefix)

strip(), lstrip() and rstrip() only remove characters from the ends of a string. Their argument is a set of characters, not a substring, which surprises many people:

url = "www.example.com"

print(url.strip("w.com"))             # strips any of w . c o m from BOTH ends: not a substring!
print(url.removeprefix("www."))       # removes the exact prefix (Python 3.9+)
print(url.removesuffix(".com"))       # removes the exact suffix

code = "xx--ID-2025--xx"
print(code.strip("x-"))               # remove several characters from the ends only
print(code.lstrip("x-"), "|", code.rstrip("x-"))

Output:

example
example.com
www.example
ID-2025
ID-2025--xx | xx--ID-2025
Command Prompt showing Python strip('w.com') returning example, compared with removeprefix and removesuffix and strip with several characters
strip("w.com") removed www. and .com, because it strips any of those letters.

Use removeprefix() / removesuffix() when you mean an exact piece of text at the start or end. For line breaks, see remove newlines from a string.

Remove a substring or a whole word

import re

sentence = "Please remove the word very from this very long sentence."

print(sentence.replace("very ", ""))                    # remove a substring
print(re.sub(r"\s*\bvery\b", "", sentence))            # remove a whole word and the space before it
print(" ".join(sentence.replace("very", "").split()))   # remove, then fix double spaces

Output:

Please remove the word from this long sentence.
Please remove the word from this long sentence.
Please remove the word from this long sentence.

The regular expression with \b (word boundary) removes very only as a whole word, and \s* takes the space in front of it too.

Remove digits, letters or symbols

Use a character class in re.sub(), the constants in the string module, or a test such as isalpha():

import re
import string

text = "Order #A-1042 shipped on 2025-07-04 to Chicago!"

print(re.sub(r"\d", "", text))                                  # remove all digits
print(text.translate(str.maketrans("", "", string.digits)))      # same with translate
print(re.sub(r"[^A-Za-z0-9 ]", "", text))                        # keep letters, digits, spaces
print("".join(ch for ch in text if ch.isalpha() or ch == " "))   # letters and spaces only

Output:

Order #A- shipped on -- to Chicago!
Order #A- shipped on -- to Chicago!
Order A1042 shipped on 20250704 to Chicago
Order A shipped on  to Chicago

Remove characters from every string in a list or pandas column

import pandas as pd

codes = ["A-101", "B-202", "C-303"]
print([c.replace("-", "") for c in codes])          # every string in a list

df = pd.DataFrame({"price": ["$1,200", "$950", "$12,499"]})
df["price_num"] = df["price"].str.replace(r"[$,]", "", regex=True).astype(int)   # whole column
print(df)

Output:

['A101', 'B202', 'C303']
     price  price_num
0   $1,200       1200
1     $950        950
2  $12,499      12499

Common mistake: forgetting to assign the result

name = "J@ne"
name.replace("@", "a")          # strings are immutable: the result is thrown away
print(name)

name = name.replace("@", "a")   # assign the result
print(name)

Output:

J@ne
Jane

None of the string methods change the original string. If your string looks unchanged, check that you assigned the returned value.

Which method is fastest?

Timing the four approaches on a 6,000-character string (2,000 runs each):

import re
import timeit

text = "Price: $1,299.99 (incl. tax)! " * 200
remove = "$,()!"
table = str.maketrans("", "", remove)
pattern = re.compile(r"[$,()!]")

def with_replace(s):
    for ch in remove:
        s = s.replace(ch, "")
    return s

tests = {
    "translate": lambda: text.translate(table),
    "replace loop": lambda: with_replace(text),
    "re.sub": lambda: pattern.sub("", text),
    "join + generator": lambda: "".join(ch for ch in text if ch not in remove),
}
for name, fn in tests.items():
    seconds = min(timeit.repeat(fn, number=2000, repeat=3))
    print(f"{name:<17} {seconds * 1000:7.1f} ms for 2,000 runs")

Output (one run; the screenshot below is a second run, so the numbers differ slightly):

translate             9.8 ms for 2,000 runs
replace loop         30.3 ms for 2,000 runs
re.sub              164.1 ms for 2,000 runs
join + generator    402.4 ms for 2,000 runs
Command Prompt timing results comparing translate, a replace loop, re.sub and join for removing several characters from a long string
One run on a Windows PC; your numbers will differ, but the order is typically the same.
SituationBest choice
One character or one substringreplace()
Several single characterstranslate(str.maketrans("", "", chars))
Patterns (digits, words, anything but letters)re.sub()
Characters only at the start or endstrip(), removeprefix(), removesuffix()
A positionSlicing: s[:i] + s[i + 1:]

More Python string guides:

Frequently asked questions

How do I remove a character from a string in Python?

Use text = text.replace("x", ""). It returns a new string without any x; pass a third argument to remove only the first n occurrences.

How do I remove multiple characters from a string?

text.translate(str.maketrans("", "", "abc")) removes every a, b and c in one pass. re.sub(r"[abc]", "", text) does the same with a regular expression.

How do I strip multiple characters in Python?

Pass them all to strip(): text.strip("-x "). It removes any of those characters from both ends only. To remove an exact prefix or suffix, use removeprefix() or removesuffix().

How do I remove a character at a specific index?

Slice around it: text[:i] + text[i + 1:]. Use text[1:] for the first character and text[:-1] for the last.

How do I remove a substring from a string?

text.replace("substring", ""). For a whole word only, use re.sub(r"\bword\b", "", text).

Why does replace() not change my string?

Strings are immutable: replace() returns a new string. Assign it, for example text = text.replace("a", "").