Remove Parentheses From String in Python

  1. Remove Parentheses From a String With String Manipulation in Python
  2. Remove Parentheses From a String With Regular Expressions in Python

This tutorial will demonstrate the methods to remove a set of parentheses from a specific string in Python.

Remove Parentheses From a String With String Manipulation in Python

By default, we have many functions available to perform string manipulation in Python. For this specific problem, we can utilize the replace() function in Python.

The replace() function replaces a string or character inside our main string with another string or character. This function takes two arguments: the old value and the new value. It replaces the old value with the new value and returns the new string.

The following example code shows us how to remove a set of parentheses from our string with the replace() function.

string = "(This is (a) string)"
string = string.replace("(","").replace(")","")
print(string)

Output:

This is a string

We removed the parentheses inside our string variable using the string.replace() function in the code above. We achieved our goal by replacing the opening and closing parentheses with an empty string and storing the return value inside our original string.

Remove Parentheses From a String With Regular Expressions in Python

We can also achieve the same output as our previous example by using regular expressions in Python. To use regular expressions, we first have to import the re module into our code. The re.sub() function can be used here.

This function takes the pattern, the replacement, and the original string; it replaces the pattern with the replacement inside the original string and returns the output.

The following example program shows us how to use the re.sub() function to remove parentheses from a string.

import re
string = "(This is (a) string)"
string = re.sub("[()]","", string)
print(string)

Output:

This is a string

We removed parentheses from our string variable using the re.sub() function in the code above. We achieved our goal by replacing the opening and closing parentheses with an empty string and storing the return value inside our original string.

Although this approach is a little complex and requires some understanding of the basic concept of regular expressions in Python, this approach is the most recommended one. Regular expressions have a wide range of applications compared to the simple replace() function for string. Every good Python programmer must at least have a basic understanding of regular expressions.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python String

  • Remove From the String in Python
  • Convert List of Strings to Integer in Python
  • Related Article - Python Regex

  • Read File to a String in Python
  • Split String in Half in Python