To remove a character from a string in Python, create a new string with the unwanted character left out. Python strings are immutable, so methods such as replace(), translate(), slicing, strip(), and regular expressions return new strings instead of editing the original object in place.
The best method depends on what you are removing. Use replace() for one known character or substring, translate() for several exact characters, slicing for one position, strip() for characters at the edges, and re.sub() for a pattern such as punctuation or non-alphanumeric text.
Remove one character with replace()
str.replace(old, new) is the simplest choice when you know the exact character or substring you want to remove. Replace it with an empty string.
text = "remove all r from this string"
result = text.replace("r", "")
print(result)
emove all fom this sting
This removes every lowercase r. If case matters, handle uppercase and lowercase separately, or normalize the string first. Our Python lowercase guide explains that normalization step.
Remove multiple characters with translate()
When you need to delete several exact characters, str.translate() with str.maketrans() is usually cleaner and faster than chaining many replace() calls.
text = "a-b+c!"
remove_chars = "-+!"
delete_table = str.maketrans("", "", remove_chars)
clean = text.translate(delete_table)
print(clean)
abc
The third argument to str.maketrans() lists characters to delete. For more examples, see our focused Python translate guide.
Remove characters by position with slicing
If you want to remove the character at a known index, join the part before the index with the part after it. This keeps all other characters unchanged.
word = "Python"
index = 2
result = word[:index] + word[index + 1:]
print(result)
Pyhon
Slicing is precise, but it is only safe when the position is known. If you are searching before removing, first confirm where the character appears; our find character in string guide covers those checks.
Remove edge characters with strip()
Use strip(), lstrip(), or rstrip() when the unwanted characters are only at the beginning or end of the string. This is common for whitespace, hashes, quotes, or delimiters copied from user input.
messy = " ##Python## "
clean = messy.strip(" #")
print(clean)
Python
strip(" #") removes spaces and hash characters from both ends. It does not remove matching characters from the middle. For whitespace-specific cleanup, see remove whitespace using Python.
Remove prefixes and suffixes
For an exact beginning or ending string, removeprefix() and removesuffix() are more readable than manual slicing.
print("unhappy".removeprefix("un"))
print("report.csv".removesuffix(".csv"))
happy report
Remove characters with regular expressions
Use re.sub() when the removal rule is pattern-based. The following example removes punctuation while keeping letters, numbers, and spaces.
import re
text = "Python, Pool! 2026?"
clean = re.sub(r"[^A-Za-z0-9 ]+", "", text)
print(clean)
Python Pool 2026
Regular expressions are powerful, but they can become hard to read. Use them when a character class or pattern is clearer than several separate string operations. Related examples include removing punctuation, removing Unicode characters, and regex newline handling.
Filter characters with join()
A generator expression with join() is useful when the rule is easier to write as a condition. This example keeps only letters.
text = "A1B2C3"
letters_only = "".join(ch for ch in text if ch.isalpha())
print(letters_only)
ABC
Common mistakes
- Expecting the original string to change. Assign the returned value to a variable.
- Using
strip()when the unwanted character is in the middle of the string. - Removing only lowercase text when uppercase variants also exist.
- Using regex for simple exact replacements where
replace()is clearer. - Forgetting that
replace()removes every match unless you pass a count.
Which method should you choose?
Choose replace() for one exact value, translate() for several exact single characters, slicing for one known position, strip() for edge cleanup, and re.sub() for patterns. If the code will be maintained by someone else, prefer the method that makes the removal rule obvious at a glance.
Remove only the first occurrence
replace() accepts a third argument that limits how many matches are removed. This is useful when the first marker is metadata, a prefix-like value, or a single typo, but later matches should stay in the string.
text = "error report"
print(text.replace("r", "", 1))
print(text.replace("r", ""))
eror report eo epot
Watch case sensitivity
String removal is case-sensitive by default. Removing lowercase p will not remove uppercase P. If users may enter mixed-case text, decide whether case should be preserved, normalized, or handled with a case-insensitive regular expression.
For example, text.replace("P", "") and text.replace("p", "") are different operations. That distinction is important when cleaning names, codes, URLs, and labels where uppercase letters can carry meaning.
Performance notes for long strings
For short text, readability matters more than micro-optimizing. For very long strings or repeated cleanup in a loop, prefer translate() for deleting many single characters and prefer one compiled regular expression for pattern-based cleanup. Avoid building a result with repeated += inside a loop when a generator expression and join() communicate the same rule more clearly.
Related Python guides
- Python translate()
- Remove the last character from a string
- Python string __contains__()
- Python substring examples
- What split() does in Python
- Fix string item assignment errors