Python
remove character
string manipulation
Python programming
coding tutorial

How to delete a character from a string using Python

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Python, you do not delete characters from a string in place because strings are immutable. Instead, you build a new string that omits the character you want to remove. The best method depends on whether you want to remove by position, remove all matching characters, or remove only the first occurrence.

Remove a Character by Index

If you know the character position, slicing is the most direct solution.

python
1text = "python"
2index = 2
3
4result = text[:index] + text[index + 1:]
5print(result)  # pyhon

This works because:

  • 'text[:index] gives everything before the character'
  • 'text[index + 1:] gives everything after it'

Together they form the new string without the targeted character.

Remove All Occurrences of a Character

If the goal is "remove every a from the string," use replace.

python
text = "banana"
result = text.replace("a", "")
print(result)  # bnn

This is concise and readable when the rule is literal replacement.

Remember that replace removes all occurrences unless you pass a count.

Remove Only the First Occurrence

If you want just one removal:

python
text = "banana"
result = text.replace("a", "", 1)
print(result)  # bnana

That count parameter is useful when the string contains repeated characters and only the first one should disappear.

Remove Characters Conditionally

Sometimes the real requirement is not one exact character but a rule such as:

  • remove digits
  • remove whitespace
  • remove punctuation

For that, a comprehension or generator expression is often clearer.

python
text = "a1b2c3"
result = "".join(ch for ch in text if not ch.isdigit())
print(result)  # abc

This is more flexible than chaining several replace calls.

Remove by Position Safely

If the index may be invalid, validate it first.

python
1def remove_at(text: str, index: int) -> str:
2    if index < 0 or index >= len(text):
3        raise IndexError("index out of range")
4    return text[:index] + text[index + 1:]
5
6
7print(remove_at("python", 2))

That is better than silently returning the original string when the caller provided a bad position, unless silent behavior is explicitly what your application wants.

Convert to a List Only If You Need Many Mutations

For repeated character removals by position, converting to a list can be practical.

python
1chars = list("python")
2del chars[2]
3result = "".join(chars)
4print(result)  # pyhon

This is not necessary for a single deletion, but it can be useful if you are doing many position-based edits before rebuilding the string.

Do not use this form by default unless the workflow really involves multiple mutations.

Use translate for Character Sets

If you need to remove several specific characters efficiently, translate is a strong built-in option.

python
1text = "a-b_c:d"
2table = str.maketrans("", "", "-_:")
3result = text.translate(table)
4print(result)  # abcd

This is often cleaner than many repeated replace calls when the rule is "drop any character from this set."

Unicode and Visual Characters

Be careful with what you think a "character" is. Python strings are Unicode, and some user-visible glyphs are composed of multiple code points. Simple slicing and removal work at the code-point level, which is usually fine but not always the same as removing one displayed symbol.

That matters mainly in advanced text-processing cases such as:

  • emojis
  • combining accents
  • grapheme-cluster-aware editors

For everyday ASCII-like cases, the standard methods above are usually enough.

Common Pitfalls

  • Trying to delete from a string in place as if it were a mutable sequence.
  • Using replace when you really need to remove by index.
  • Forgetting that replace removes all occurrences unless given a count.
  • Skipping index validation in helper functions that remove by position.
  • Assuming one displayed symbol always equals one simple Python character position.

Summary

  • Python strings are immutable, so deletion always creates a new string.
  • Use slicing to remove a character by position.
  • Use replace to remove matching characters by value.
  • Use generator expressions or translate for rule-based removal.
  • Pick the method based on whether the removal is positional, literal, or conditional.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.