Python Remove Multiple Characters From String

Recently, while cleaning up a dataset for a project, I ran into a problem where I needed to remove unwanted characters from text strings.

At first, I thought this would be simple, but then I realized Python gives us several different ways to handle this task. Some methods are quick and simple, while others are more powerful and flexible.

In this tutorial, I’ll show you five easy methods I use to remove multiple characters from a string in Python. I’ll also share complete code examples so you can try them out right away.

Method 1 – Use replace() Multiple Times

The first method I often use is Python’s built-in replace() function. It’s easy and works perfectly when you only need to remove a few known characters.

# Example: Cleaning up a US phone number string
text = "(123)-456-7890"

# Remove parentheses and hyphens
cleaned = text.replace("(", "").replace(")", "").replace("-", "")

print("Original:", text)
print("Cleaned :", cleaned)

Output:

Original: (123)-456-7890
Cleaned : 1234567890

I executed the above example code and added the screenshot below.

python remove character from string

This method is simple, but if you have too many characters to remove, it can get repetitive.

Method 2 – Use translate() with str.maketrans()

When I need to remove multiple characters at once, I prefer using translate(). It’s faster and cleaner than chaining replace() calls in Python.

# Example: Removing unwanted punctuation from text
text = "Hello, USA! Welcome to Python."

# Characters to remove
remove_chars = ",!."

# Create translation table
table = str.maketrans("", "", remove_chars)

# Apply translate
cleaned = text.translate(table)

print("Original:", text)
print("Cleaned :", cleaned)

Output:

Original: Hello, USA! Welcome to Python.
Cleaned : Hello USA Welcome to Python

I executed the above example code and added the screenshot below.

python replace multiple characters

This is one of the most efficient methods for bulk character removal.

Method 3 – Use Regular Expressions (re.sub())

If I’m working with text where patterns matter, I use Python’s re module. It allows me to remove all unwanted characters with just one line.

import re

# Example: Removing all non-alphabetic characters
text = "Python 3.11 is awesome in the USA!!!"

# Keep only letters and spaces
cleaned = re.sub(r"[^A-Za-z\s]", "", text)

print("Original:", text)
print("Cleaned :", cleaned)

Output:

Original: Python 3.11 is awesome in the USA!!!
Cleaned : Python  is awesome in the USA

I executed the above example code and added the screenshot below.

python strip multiple characters

This method is extremely powerful when dealing with messy datasets.

Method 4 – Use List Comprehension

Sometimes, I like to use list comprehension because it’s very efficient and gives me more control.

# Example: Removing vowels from a string
text = "Data Cleaning in Python"

# Characters to remove
remove_chars = "aeiouAEIOU"

# Build a new string
cleaned = "".join([ch for ch in text if ch not in remove_chars])

print("Original:", text)
print("Cleaned :", cleaned)

Output:

Original: Data Cleaning in Python
Cleaned : Dt Clnng n Pythn

This method is flexible and easy to customize.

Method 5 – Use join() with filter()

Another trick I use is combining join() with filter() methods in Python. This works well when you want to keep only certain characters.

# Example: Keep only digits from a string
text = "Order ID: #US12345"

# Keep only digits
cleaned = "".join(filter(str.isdigit, text))

print("Original:", text)
print("Cleaned :", cleaned)

Output:

Original: Order ID: #US12345
Cleaned : 12345

This approach is concise and very readable.

Which Method Should You Use?

  • Use replace() if you only need to remove a few known characters.
  • Use translate() for efficient bulk removal.
  • Use regex when dealing with patterns or complex text cleaning.
  • Use list comprehension for custom filtering logic.
  • Use filter() when you want to keep only certain types of characters.

In my experience working with Python, I’ve found that there’s no single “best” way to remove multiple characters from a string.

If I’m cleaning up phone numbers or IDs, I usually go with translate(). For messy text data, regex is my go-to. And when I want something quick and readable, I use list comprehension.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.