How to Remove a Character from a String in Python

What you'll build or solve

You'll remove a character from a Python string in a way that matches your goal, such as deleting one specific character, removing a character at a position, or stripping unwanted characters from the ends.

When this approach works best

Removing a character works well when you:

  • Clean user input, like removing # from a tag or , from a number.
  • Normalize identifiers, like deleting hyphens from "AB-123-456" before matching.
  • Fix formatting from copied text, like removing stray quotes or invisible characters.

Avoid character removal when the text needs structure-aware parsing, like editing CSV data or JSON by hand. In those cases, use a parser instead of manual string edits.

Prerequisites

  • Python installed
  • You know what a string is

Step-by-step instructions

1) Remove all occurrences of a character with replace()

Use replace() when you want to delete a character everywhere in the string.

s = "AB-123-456"
clean = s.replace("-", "")
print(clean)

Option A: Remove only the first occurrence

s = "2026/02/17"
clean = s.replace("/", "-", 1)
print(clean)

Option B: Remove a set of characters

Loop over the characters you want to delete.

s = "(555) 123-456"
to_remove = "()- "

for ch in to_remove:
    s = s.replace(ch, "")

print(s)

What to look for:

replace() returns a new string. Save the result if you plan to use it later.


2) Remove a character at a specific index with slicing

Use slicing when you know the position you want to remove.

s = "pyt hon"
i = 3  # remove the space at index 3

result = s[:i] + s[i + 1:]
print(result)

If the index might be out of range, check it first.

s = "hi"
i = 10

if 0 <= i < len(s):
    s = s[:i] + s[i + 1:]

print(s)

What to look for:

Slicing stays safe for ranges, but direct indexing like s[i] raises IndexError when i is out of range.


3) Remove a character from the start or end with strip()

Use strip(), lstrip(), or rstrip() when the unwanted characters appear at the edges.

s = '"hello"'
clean = s.strip('"')
print(clean)

Option A: Remove common whitespace at the edges

s = "  hello\n"
clean = s.strip()
print(clean)

Option B: Remove multiple different edge characters

Pass a string of characters to remove.

s = "---title---"
clean = s.strip("-")
print(clean)

What to look for:

strip() removes characters from both ends until it hits a character that is not in the removal set. It does not remove characters from the middle.


4) Remove characters by rule with a filter

Use this when you want to keep only certain kinds of characters, like digits or letters.

s = "Order #A-19/26"
digits_only = "".join(ch for ch in s if ch.isdigit())
print(digits_only)

Option A: Keep letters and digits only

s = "User: mina@example.com!"
kept = "".join(ch for ch in s if ch.isalnum())
print(kept)

What to look for:

Filtering gives you full control, but it can remove punctuation you meant to keep. Keep the rule tight.


Examples you can copy

Example 1: Remove a currency symbol

price = "€199"
clean = price.replace("€", "")
print(clean)

Example 2: Remove hyphens from an ID

order_id = "AB-123-456"
normalized = order_id.replace("-", "")
print(normalized)

Example 3: Remove a character at a position found by code

s = "INV#2026"
hash_pos = s.find("#")

if hash_pos != -1:
    s = s[:hash_pos] + s[hash_pos + 1:]

print(s)

Example 4: Keep digits only from a phone number

phone = "(555) 123-4567"
digits = "".join(ch for ch in phone if ch.isdigit())
print(digits)

Example 5: Remove quotes only if they wrap the whole value

value = '"hello world"'

if value.startswith('"') and value.endswith('"') and len(value) >= 2:
    value = value[1:-1]

print(value)

Common mistakes and how to fix them

Mistake 1: Using strip() to remove characters in the middle

What you might do

s = "AB-123-456"
print(s.strip("-"))

Why it breaks

strip() only removes characters from the start and end, not the middle.

Fix

s = "AB-123-456"
print(s.replace("-", ""))

Mistake 2: Removing characters at an index without bounds checking

What you might do

s = "hi"
i = 10
s = s[:i] + s[i + 1:]
print(s)

Why it breaks

The slice does not crash, but the result is unchanged. You may think it worked when it did not.

Fix

s = "hi"
i = 10

if 0 <= i < len(s):
    s = s[:i] + s[i + 1:]

print(s)

Troubleshooting

If you see AttributeError on replace() or strip(), confirm your value is a string, not None or a number.

If your output does not change, print repr(s) and confirm the character you want to remove matches exactly, including invisible characters.

If you get IndexError, you used direct indexing like s[i] with a bad index. Use slicing plus a bounds check.

If you remove too much, switch from replace() to a filter rule that keeps what you want.

If you need to remove a substring, not a single character, use replace("target", "") or slicing around the substring indexes.


Quick recap

  • Use replace(char, "") to remove a character everywhere.
  • Use replace(char, "", 1) to remove only the first occurrence.
  • Use slicing to remove a character at a specific index: s[:i] + s[i+1:].
  • Use strip() to remove characters from the start or end.
  • Use a filter with join() when you want rule-based removal, like keeping digits only.