w3resource

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 

Pictorial Presentation:

Python: Remove the parenthesis area in a string.

Flowchart:

Flowchart: Regular Expression - Remove the parenthesis area in a string.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to remove words from a string of length between 1 and a given number.
Next: Write a Python program to insert spaces between words starting with capital letters.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

For-else construct useful when searched for something and find it:

# For example assume that I need to search through a list and process each item until a flag item is found and 
# then stop processing. If the flag item is missing then an exception needs to be raised.

for i in mylist:
    if i == theflag:
        break
    process(i)
else:
    raise ValueError("List argument missing terminal flag.")