from pyparsing import nestedExpr
import sys
s = 'AX(p>q)&E((-p)Ur)'
expr = nestedExpr('(', ')')
result = expr.parseString('(' + s + ')').asList()[0]
s = ''.join([x for x in result if isinstance(x, str)])
print(s)
python string manipulation