• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Remove Brackets from String Using Python

Remove Brackets from String Using Python

February 15, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Swap Two Values of Variables in Python
  • 2.  Are Tuples Mutable in Python? No, Tuples are not Mutable
  • 3.  Python Add Months to Datetime Variable Using relativedelta() Function
  • 4.  Python isdigit() Function – Check if Characters in String are Digits
  • 5.  Check if Variable is Float in Python
  • 6.  pandas idxmax – Find Index of Maximum Value of Series or DataFrame
  • 7.  Using Python to Initialize Array of Size N
  • 8.  How to Remove All Punctuation from String in Python
  • 9.  How to Subtract Two Numbers in Python
  • 10.  Convert Set to List in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy