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:

# 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

Reply via email to