How to check if a string is empty in python?
Dustan
DustanGroups at gmail.com
Thu May 3 07:07:27 EDT 2007
More information about the Python-list mailing list
Thu May 3 07:07:27 EDT 2007
- Previous message (by thread): How to check if a string is empty in python?
- Next message (by thread): How to check if a string is empty in python?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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: > > # 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.
- Previous message (by thread): How to check if a string is empty in python?
- Next message (by thread): How to check if a string is empty in python?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list