How to remove text inside brackets in Python? - GeeksforGeeks

27 Mar, 2021
In this article, we will learn how to remove content inside brackets without removing brackets in python.
Examples:
Input: (hai)geeks
Output: ()geeks

Input: (geeks)for(geeks)
Output: ()for()
We can remove content inside brackets without removing brackets in 2 methods, one of them is to use the inbuilt methods from the re library and the second method is to implement this functionality by iterating the string using a for loop
Method 1: We will use sub() method of re library (regular expressions). 
sub(): The functionality of sub() method is that it will find the specific pattern and replace it with some string.
This method will find the substring which is present in the brackets or parenthesis and replace it with empty brackets.
Approach :
Import the re libraryNow find the sub-string present in the brackets and replace with () using sub() method.We need to pass the sub() method with 2 arguments those are pattern and string to be replaced with.Print the string.
Import the re library
Now find the sub-string present in the brackets and replace with () using sub() method.
We need to pass the sub() method with 2 arguments those are pattern and string to be replaced with.
Print the string.
In the below code \(.*?\) represents the regular expression for finding the brackets containing some content. The brackets () have some special meaning in regular expression in python so Backlash \ is used to escape that meaning. 
Python3
# Importing moduleimport re  # Input stringstring="(Geeks)for(Geeks)"  # \(.*?\) ==> it is a regular expression for finding# the pattern for brackets containing some contentstring=re.sub("\(.*?\)","()",string)  # Output stringprint(string)
()for()
Time complexity: O(2^m + n).
Where m is the size of the regex, and n is the size of the string. Here the sub() method will take 2^m time to find the pattern using the regex and O(n) to iterate through the string and replace with the “()”.
Method 2: In this method, we will iterate through the string and if the character that is being iterated is not present in between the parenthesis then the character will be added to the resultant string.
If there is an open or closed parenthesis present in the string then the parenthesis will be added to the resultant string but the string in between then is not added to the resultant string.
Approach:
Iterate the loop for each character in the string.If a ‘(‘ or ‘)’ appears in the string we will add it to the result string.If the parenthesis number in the string is zero then add the character to the result string.Here if the parenthesis number is greater than zero it indicates that the current character which is being iterated is present in between two parenthesesPrint the result string.
Iterate the loop for each character in the string.
If a ‘(‘ or ‘)’ appears in the string we will add it to the result string.
If the parenthesis number in the string is zero then add the character to the result string.
Here if the parenthesis number is greater than zero it indicates that the current character which is being iterated is present in between two parentheses
Print the result string.
Python3
# Input stringstring="geeks(for)geeks"  # resultant stringresult = ''  # paren counts the number of brackets encounteredparen= 0for ch in string:          # if the character is ( then increment the paren    # and add ( to the resultant string.    if ch == '(':        paren =paren+ 1        result = result + '('          # if the character is ) and paren is greater than 0,     # then increment the paren and     # add ) to the resultant string.    elif (ch == ')') and paren:        result = result + ')'        paren =paren- 1          # if the character neither ( nor  then add it to    # resultant string.    elif not paren:        result += ch  # print the resultant string.print(result)
geeks()geeks
Time complexity : O(n).
Here n is the length of the string. In the code, we are iterating through the string and appending the content outside of the parenthesis so it would only take the time O(n).
Picked
python-regex
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
sum() function in Python
Create a Pandas DataFrame from Lists
How to drop one or multiple columns in Pandas Dataframe
*args and **kwargs in Python
Graph Plotting in Python | Set 1