Remove Brackets from Python Strings

Recently, while working on a data-cleaning project, I ran into a problem where my dataset had unnecessary square brackets around values.

For example, instead of “New York”, the data looked like “[New York]”. If you’ve ever imported lists or JSON data into Python, you’ve probably seen this happen too.

At first glance, it looks small, but when you’re dealing with thousands of rows, removing these brackets quickly becomes essential.

In this tutorial, I’ll show you five simple methods I use to remove brackets from Python strings. Each method has its own use case, and I’ll explain them step by step so you can pick the one that works best for you.

Method 1: Use the replace() method

We can easily remove brackets by replacing them with an empty string using the str.replace() method in Python. This method is simple to understand, making it ideal for simple bracket removal tasks.

Consider the following example:

def remove_brackets(string, open_bracket, close_bracket):
    return string.replace(open_bracket, "").replace(close_bracket, "")

string_with_brackets = "(United) States (of) America"
open_bracket = "("
close_bracket = ")"
string_without_brackets = remove_brackets(string_with_brackets, open_bracket, close_bracket)
print(string_without_brackets)

Output:

United States of America

You can see the output in the screenshot below.

How to remove brackets from Python string

This method removes brackets by replacing them with an empty string, making it a simple and effective approach for basic bracket removal.

Method 2: Use join and filter methods

In Python, we can combine the filter() function with list comprehension to exclude brackets from the string.

Here’s a simple example:

def remove_brackets(string, open_bracket, close_bracket):
    return ''.join(filter(lambda x: x not in [open_bracket, close_bracket], string))

string_with_brackets = "Python is (my favorite) Programming Language"
open_bracket = "("
close_bracket = ")"
string_without_brackets = remove_brackets(string_with_brackets, open_bracket, close_bracket)
print(string_without_brackets)

Output:

Python is my favorite Programming Language

You can see the output in the screenshot below.

Remove brackets from a String in Python

We can easily filter out the brackets from a string and convert it into a list using the filter function with list comprehension, applying conditions with a lambda function in Python. and then join each element of the list.

Method 3: Use list comprehension with join()

We can use list comprehension to iterate through the string characters and join only those substrings, excluding brackets, with the help of the join() function in Python.

Let’s see an example:

def remove_brackets(string, open_bracket, close_bracket):
    return ''.join(char for char in string if char not in [open_bracket, close_bracket])

string_with_brackets = "In USA, ((innovation) drives) technological advancements)."
open_bracket = "("
close_bracket = ")"
string_without_brackets = remove_brackets(string_with_brackets, open_bracket, close_bracket)
print(string_without_brackets)

Output

In USA, innovation drives technological advancements.

You can see the output in the screenshot below.

Remove Parentheses from String in Python

This method removes brackets by filtering them out with list comprehension and joining the remaining characters, offering a clean and efficient solution.

Method 4: Use Python regular expression

We can also use the regular expressions with the re.sub() function in Python to substitute brackets with an empty string.

For instance:

import re

def remove_brackets(string, open_bracket, close_bracket):
    return re.sub(r'[{}{}]'.format(re.escape(open_bracket), re.escape(close_bracket)), '', string)

string_with_brackets = "[USA] [offers] opportunities [for] diverse [lifestyles]"

open_bracket = "["
close_bracket = "]"
string_without_brackets = remove_brackets(string_with_brackets, open_bracket, close_bracket)
print(string_without_brackets)

Output:

USA offers opportunities for diverse lifestyles

You can see the output in the screenshot below.

Remove brackets from the string using translate() method in python

Regular expressions in Python provide flexibility and allow us to handle more complex patterns and multiple types of brackets easily.

Method 5: Use the translate() method

We can also create a translation table with str.maketrans() in Python and remove brackets from the string using str.translate().

Here’s a simple example:

def remove_brackets(string, open_bracket, close_bracket):
    table = str.maketrans('', '', open_bracket + close_bracket)
    return string.translate(table)

string_with_brackets = "USA's cities {pulsate} with vibrant {energy}."

open_bracket = "{"
close_bracket = "}"
string_without_brackets = remove_brackets(string_with_brackets, open_bracket, close_bracket)
print(string_without_brackets)

Output:

USA's cities pulsate with vibrant energy.

You can see the output in the screenshot below.

Remove brackets from the string using translate() method in python

As str.translate() method in Python offers high performance when dealing with large strings.

In summary, we explored five methods to remove brackets from a string in Python. These methods include using the replace() method, join and filter methods, list comprehension with join(), regular expressions, and the translate() method.

Each method offers its own advantages. Depending on the specific requirements of our task and the size of our data, we can choose the most suitable method to clean text data by removing brackets from a Python string.

You may also like to 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.