Placement Prep

Python: Remove a Character from a String (3 Methods)

Three Python methods to remove a character from a string: str.replace(), list comprehension with join(), and str.translate(). Code examples and edge cases included.

By FACE Prep Team 4 min read
python string-manipulation python-programs placement-prep string-methods interview-questions

Python gives you three clean ways to remove a character from a string: str.replace(), list comprehension with join(), and str.translate(). Each suits a different scenario.

None of them modifies the original string in place. Python strings are immutable, and every method returns a new string.

What “Removing a Character” Means in Python

A Python string is a sequence of Unicode characters stored as an immutable object. You cannot change individual characters the way you would modify a list element. Every string operation returns a brand-new string and leaves the original untouched.

This trips up students writing code like:

s = "placement"
s.replace("e", "")
print(s)  # still "placement"

The replace() call succeeds, but the result is discarded. Assign it back:

s = "placement"
s = s.replace("e", "")
print(s)  # "placmnt"

That reassignment pattern applies to every method in this article. The string name s now points to the new string object; the old "placement" still exists in memory until garbage-collected.

Method 1: Using str.replace()

The str.replace(old, new, count=-1) method scans the string left-to-right and substitutes every occurrence of old with new. Pass an empty string for new to remove the character:

# Remove all occurrences of 'e'
s = "placement"
result = s.replace("e", "")
print(result)  # "placmnt"

By default, replace() removes every occurrence. The optional count argument caps how many replacements happen:

# Remove only the first occurrence of 'e'
s = "placement"
result = s.replace("e", "", 1)
print(result)  # "placment"

str.replace() is the default choice for removing a single character by value. It needs no import, reads clearly, and handles the most common case in one call. The method is case-sensitive: s.replace("E", "") will not touch lowercase "e".

Method 2: List Comprehension and join()

The list-comprehension approach converts the string to a sequence, filters each character, and joins the result back:

s = "placement"
char_to_remove = "e"
result = "".join([c for c in s if c != char_to_remove])
print(result)  # "placmnt"

The filter condition can be anything, not just value equality. That flexibility is what distinguishes this approach from replace(). Remove digits, keep only alphabetic characters, drop vowels: list comprehension handles all of these without a separate import.

# Keep only alphabetic characters
s = "face2026prep"
result = "".join([c for c in s if c.isalpha()])
print(result)  # "faceprep"

You can also write the comprehension as a generator expression by dropping the square brackets: "".join(c for c in s if c != char_to_remove). Both produce identical output. The generator form skips building an intermediate list in memory, which makes a difference on strings with hundreds of thousands of characters. For typical placement-test inputs, the difference is negligible.

Placement coding tests at Infosys InfyTQ and Cognizant GenC sometimes ask for exactly this kind of conditional filtering. Knowing the pattern saves time in timed test environments.

Method 3: Using str.translate()

str.translate() maps each character through a translation table. The helper str.maketrans() builds that table. Pass the characters to delete as the third argument:

s = "placement"
char_to_remove = "e"
table = str.maketrans("", "", char_to_remove)
result = s.translate(table)
print(result)  # "placmnt"

The str.maketrans(x, y, z) call takes three strings: x is the “from” set, y is the “to” set (each character in x maps to the corresponding character in y), and z is a set of characters to delete outright. Passing empty strings for x and y means no substitutions; only the deletions from z apply. The resulting table is a dictionary mapping Unicode ordinals to their replacements or to None (deletion).

The real advantage of str.translate() appears when you need to remove multiple different characters in one pass:

# Strip digits and hyphens together
s = "face-2026-prep"
chars_to_remove = "0123456789-"
table = str.maketrans("", "", chars_to_remove)
result = s.translate(table)
print(result)  # "faceprep"

A single translate() call handles all deletions simultaneously. Chaining multiple replace() calls would produce the same output but scans the string once per call.

Removing a Character by Index

The three methods above remove by value. To remove the character at a specific position, use string slicing:

def remove_at_index(s, i):
    return s[:i] + s[i + 1:]

s = "FACE Prep"
print(remove_at_index(s, 3))  # "FAC Prep"
  • s[:i] collects every character before index i.
  • s[i + 1:] collects every character after index i.
  • Concatenating them skips the character at position i.

For production-quality code, add a bounds check:

def remove_at_index(s, i):
    if 0 <= i < len(s):
        return s[:i] + s[i + 1:]
    return s

The function returns the original string unchanged if i is out of range, rather than silently producing a wrong result.

Choosing the Right Method

ScenarioMethod
Remove all occurrences of one characterstr.replace(char, '')
Remove only the first occurrencestr.replace(char, '', 1)
Filter characters by any conditionList comprehension + join()
Remove several different characters in one passstr.translate() + str.maketrans()
Remove the character at a known indexSlice s[:i] + s[i+1:]

In timed placement tests at TCS NQT, the question is usually phrased as “given a string, return a new string with all occurrences of character X removed.” That maps directly to str.replace(X, ""). If the question adds a condition (“remove all non-alphabetic characters”), reach for the list-comprehension form. If the problem asks you to remove several characters at once, str.translate() is the cleaner solution.

String manipulation questions appear alongside sorting and searching in most campus placement syllabi. For a wider set of pattern examples, see Python Example Programs for Practice and Sorting a String in Alphabetical Order. When your removal logic depends on the type of character being processed, checking whether a character is uppercase, lowercase, a digit, or a special character is the natural companion.

The str.translate() approach scales into real text-preprocessing pipelines, where you strip punctuation and noise characters before passing text to a language model. If that applied Python direction interests you alongside placement prep, TinkerLLM covers those preprocessing patterns in a hands-on build track starting at ₹299.

Primary sources

Frequently asked questions

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

Use s.replace(char, '') to remove all occurrences, or list comprehension with join() for conditional removal.

What is the difference between str.replace() and str.translate() for character removal?

Both remove characters, but str.translate() is more efficient when removing multiple different characters at once.

Can I remove a character at a specific index in Python?

Yes. Use string slicing: s[:i] + s[i+1:] removes the character at index i without modifying the original.

How do I remove only the first occurrence of a character in Python?

Pass a count argument: s.replace(char, '', 1) replaces only the first occurrence and leaves the rest untouched.

Why can't Python modify a string in place?

Python strings are immutable objects. Every string operation returns a new string rather than changing the original.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF