How to check if a string is empty in python?

mensanator at aol.com mensanator at aol.com
Thu May 3 13:29:42 EDT 2007


On May 2, 11:59 pm, a... at mac.com (Alex Martelli) wrote:
> mensana... at aol.com <mensana... at aol.com> wrote:
>
>    ...
>
> > >>> 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.
>
> Being a string AND being non-empty is insufficient too -- just try
>
> gmpy.mpz("Hello, I am a string and definitely not empy!", 10)
>
> on for size.
>
> Alex

>>> import gmpy
>>> gmpy.mpz("Hello, I am a string and definitely not empy!", 10)

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    gmpy.mpz("Hello, I am a string and definitely not empy!", 10)
ValueError: invalid digits

But you don't get a TypeError, you get a ValueError. And you might
scratch your head over

>>> gmpy.mpz('zzz',26)

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    gmpy.mpz('zzz',26)
ValueError: invalid digits

until you remember that base 26 is 0-9a-p, not a-z.




More information about the Python-list mailing list