Python code for testing well parenthesized expression

Jeremy Sanders jeremy+complangpython at
Tue Jul 14 14:57:21 CEST 2009

candide wrote:

> I'm trying to implement in Python a function testing if an expression is
> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik"
> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not.
> My code follows at the end.
> If you have a better algorithm or a better Python code (I'm a beginner in
> the Python world), don't hesitate ...

Don't you want to just test that the number of "("s equals the number of 
")"s or am I missing the point?

>>> a='aAAA(bbb(cc)))'
>>> a.count('(') == a.count(')')


Jeremy Sanders

More information about the Python-list mailing list