• 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 Parentheses from String Using Python

Remove Parentheses from String Using Python

February 15, 2022 Leave a Comment

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

import re

string_with_parentheses = "(This is )a (string with parentheses)"

string_without_parentheses = re.sub(r"[\(\)]",'',string_with_parentheses)

print(string_without_parentheses)

#Output:
This is a string with parentheses

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

string_with_parentheses = "(This is a string with parentheses)"

string_without_parentheses = string_with_parentheses.strip("()")

print(string_without_parentheses)

#Output:
This is a string with parentheses

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 parentheses from a string variable. Parentheses in the wrong spots can make reading sentences tricky.

We can easily remove parentheses from strings in Python.

The easiest way to get rid of parentheses 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 parentheses characters, and then using the sub() function, we will replace them with an empty string.

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

import re

string_with_parentheses = "(This is )a (string with parentheses)"

string_without_parentheses = re.sub(r"[\(\)]",'',string_with_parentheses)

print(string_without_parentheses)

#Output:
This is a string with parentheses

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

If your parentheses 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 parentheses from the beginning and end of a string using Python, we pass “()” to the strip() function as shown below.

string_with_parentheses = "(This is a string with parentheses)"

string_without_parentheses = string_with_parentheses.strip("()")

print(string_without_parentheses)

#Output:
This is a string with parentheses

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

Other Articles You'll Also Like:

  • 1.  Using Python to Remove Quotes from String
  • 2.  How to Write Pickle File to AWS S3 Bucket Using Python
  • 3.  Transpose DataFrame pandas – Using the pandas transpose Function
  • 4.  Remove Every Nth Element from List in Python
  • 5.  Using Python to Flatten Array of Arrays
  • 6.  Count Spaces in String in Python
  • 7.  How to Write CSV File to AWS S3 Bucket Using Python
  • 8.  math.degrees() Python – How to Convert Radians to Degrees in Python
  • 9.  Not Equal Operator != in Python
  • 10.  Using Python to Sum Even Numbers in List

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