w3resource logo
Python Exercises

Python: Remove the parenthesis area in a string

Python Regular Expression: Exercise-50 with Solution

Write a Python program to remove the parenthesis area in a string.

Sample Solution:-

Python Code:

import re
items = ["example (.com)", "w3resource", "github (.com)", "stackoverflow (.com)"]
for item in items:
    print(re.sub(r" ?\([^)]+\)", "", item))
	

Sample Output:

example                                                                                                       
w3resource                                                                                                    
github                                                                                                        
stackoverflow 

Python Code Editor:

import re
items = ["example (.com)", "w3resource", "github (.com)", "stackoverflow (.com)"]
for item in items:
    print(re.sub(r" ?\([^)]+\)", "", item))

Improve this sample solution and post your code through Disqus