How to check if a string is empty in python?
Roy Smith
roy at panix.com
Wed May 2 21:19:54 EDT 2007
In article <1178154290.811928.208900 at h2g2000hsg.googlegroups.com>,
Dustan <DustanGroups at gmail.com> wrote:
> On May 2, 5:50 pm, Steven D'Aprano
> <s... at REMOVE.THIS.cybersource.com.au> wrote:
> > On Wed, 02 May 2007 13:35:47 -0700, noagbodjivictor wrote:
> > > How to check if a string is empty in python?
> > > if(s == "") ??
> >
> > In no particular order, all of these methods will work:
> >
> > # test s is equal to another empty string
> > if s == "":
> >
> > # assuming s is a string, test that it is empty
> > if not s:
> >
> > # test s is a string and it is empty
> > if isinstance(s, str) and not s:
> >
> > # test s has length 0
> > if len(s) == 0:
> >
> > # test the length of s evaluates as false
> > if not len(s):
> >
> > # a long way to test the length of s
> > if s.__len__() < 1:
> >
> > # a stupid way to test s is empty
> > if bool(s) == False:
> >
> > # a REALLY stupid way to test s is empty
> > if (bool(s) == False) == True:
>
> LOL
>
> > # test that appending s to itself is itself
> > if s+s == s:
> >
> > # test that s has none of any character
> > if not filter(None, [1 + s.find(chr(n)) for n in range(256)]):
> >
> > That last one is really only good for wasting CPU cycles.
>
> and the other ones are... ?
>
> > --
> > Steven.
s.join("foo") == "foo"
for c in s:
raise "it's not empty"
More information about the Python-list
mailing list