python remove text between parentheses

50

python remove text between parentheses -

# credit to the Stack Overflow user in the source link
>>> import re 
>>> x = "This is a sentence. (once a day) [twice a day]"
>>> re.sub("[\(\[].*?[\)\]]", "", x)
'This is a sentence.  '

remove all parentheses from string python -

>>> table = str.maketrans({"(":None, ")":None})

remove all parentheses from string python -

>>> table = str.maketrans(dict.fromkeys("()"))
>>> string1 = '(this) (is) (a) (test)'
>>> string1.translate(table)
'this is a test'

Comments

Submit
0 Comments