How to Remove Brackets from a Python String? [5 Ways]

Are you struggling to remove brackets from a Python string like parentheses, square brackets, or curly braces? In this Python tutorial, I will explain “How to Remove Brackets from a Python String with different methods and examples.

I wanted to clean the text data from the string where, so I was looking for methods to remove any brackets from the string and found 5 different methods to erase brackets from the string in Python.

This is the concept of data cleaning, text processing, or string formatting, where developers can use this method to get the expected result.

These are 5 methods to remove brackets from a Python string:

  1. replace() method
  2. using join and filter methods
  3. using list comprehension with join()
  4. using regular expression
  5. using translate() method

Remove brackets from a Python string using 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 straightforward and easy 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

Attached are screenshots illustrating the Python code and its corresponding output in Visual Studio

How to remove brackets from Python string

Remove brackets from a String in Python using join and filter methods

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

READ:  Python dictionary key error

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)

We can easily filter out the brackets from a string into a list using the filter function with list comprehension with conditions applied by the lambda function in Python. and then join each element of the list with the help of the join() function.

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

Output:

Python is my favorite Programming Language

Here’s a combined view of the Python script and its output within Visual Studio

Remove brackets from a String in Python

Remove Parentheses from String in Python using 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.

Displayed below is the Python code alongside its output, captured in Visual Studio

Remove Parentheses from String in Python

Remove Brackets from String Using Python using regular expression

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

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

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

Presented is a composite image showcasing both the Python code and its output in Visual

Remove brackets from the string in python

Remove brackets from the string using the translate() method

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

READ:  Python program for a diamond pattern [2 Methods]

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

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.

Enclosed is a snapshot featuring the Python script and the resulting output within Visual Studio.

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

Conclusion

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: