How to check if a string is empty in python?

mensanator at aol.com mensanator at aol.com
Wed May 2 19:46:42 EDT 2007


On May 2, 6:41 pm, Bruno Desthuilliers
<bdesth.quelquech... at free.quelquepart.fr> wrote:
> mensana... at aol.com a écrit :
>
> > On May 2, 3:49 pm, Basilisk96 <basilis... at gmail.com> wrote:
>
> >>A simple
>
> >>if s:
> >>    print "not empty"
> >>else:
> >>    print "empty"
>
> >>will do.
>
> > How do you know that s is a string?
>
> Why do you want to know if it's a string ?

>>> import gmpy
>>> gmpy.mpz(11)
mpz(11)
>>> gmpy.mpz('11',10)
mpz(11)
>>> gmpy.mpz(11,10)

Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    gmpy.mpz(11,10)
TypeError: gmpy.mpz() with numeric argument needs exactly 1 argument

The mpz conversion takes two arguments if and only if s is a string,
else it takes 1 argument. So being non-empty is insufficient.




More information about the Python-list mailing list