On May 2, 5:50 pm, Steven D'Aprano <[EMAIL PROTECTED]> 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: > > # test that appending s to itself is itself > if s+s == s:
Being the slow person that I am, it really did take me this long to find a mistake. s+s != 'appending' s+s == 'concatenation' > # 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. > > -- > Steven. -- http://mail.python.org/mailman/listinfo/python-list