w3resource

Python: Remove the parenthesis area in a string


50. Remove Parenthesis Area

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.

For more Practice: Solve these Related Problems:

  • Write a Python program to remove any text enclosed in parentheses, including the parentheses, from a list of strings.
  • Write a Python script to strip out parenthesized expressions from a string and then print the cleaned text.
  • Write a Python program to search for patterns enclosed in parentheses and remove them entirely from a given string.
  • Write a Python program to eliminate any substring within parentheses from a list of address strings.

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 Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.