To remove brackets from string using Python, the easiest way is to use the Python sub() function from the re module.

import re

string_with_brackets = "[This is ]a [string with brackets]"

string_without_brackets = re.sub(r"[[]]",'',string_with_brackets)

print(string_without_brackets)

#Output:
This is a string with brackets

If you want to get rid of square brackets and curly brackets, you can so with the following Python code.

import re

string_with_brackets = "[This is ]a [string with{} brackets]"

string_without_brackets = re.sub(r"[[{}]]",'',string_with_brackets)

print(string_without_brackets)

#Output:
This is a string with brackets

If your brackets are on the beginning and end of your string, you can also use the strip() function.

string_with_brackets = "[This is a string with brackets]"

string_without_brackets = string_with_brackets.strip("[]")

print(string_without_brackets)

#Output:
This is a string with brackets

When using string variables in Python, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove brackets from a string variable. Both square and curly brackets can make reading sentences tricky.

We can easily remove brackets from strings in Python.

The easiest way to get rid of brackets is with a regular expression search using the Python sub() function from the re module.

We can easily define a regular expression which will search for bracket characters, and then using the sub() function, we will replace them with an empty string.

Below are some examples of how you can remove brackets from string variables using Python with the sub() function.

import re

string_with_brackets = "[This is ]a [string with brackets]"

string_without_brackets = re.sub(r"[[]]",'',string_with_brackets)

print(string_without_brackets)

#Output:
This is a string with brackets

If you want to get rid of square brackets and curly brackets, you can so with the following Python code.

import re

string_with_brackets = "[This is ]a [string with{} brackets]"

string_without_brackets = re.sub(r"[[{}]]",'',string_with_brackets)

print(string_without_brackets)

#Output:
This is a string with brackets

Using strip() to Remove Brackets from the Beginning and End of Strings in Python

If your brackets are on the beginning and end of your string, you can also use the strip() function.

The Python strip() function removes specified characters from the beginning and end of a string.

To remove square brackets from the beginning and end of a string using Python, we pass “[]” to the strip() function as shown below.

string_with_brackets = "[This is a string with brackets]"

string_without_brackets = string_with_brackets.strip("[]")

print(string_without_brackets)

#Output:
This is a string with brackets

If you have curly brackets as well, we pass “[]{}” to strip() to remove the brackets.

string_with_brackets = "{[This is a string with brackets]}"

string_without_brackets = string_with_brackets.strip("[]{}")

print(string_without_brackets)

#Output:
This is a string with brackets

Hopefully this article has been useful for you to learn how to remove brackets from string using Python.

Categorized in:

Python,

Last Update: March 18, 2024