How to check if the string is empty?
Does Python have something like an empty string variable where you can do:
if myString == string.empty:
Regardless, what's the most elegant way to check for empty string values? I find hard coding
"" every time for checking an empty string not as good.
Empty strings are "falsy" which means they are considered false in a Boolean context, so you can just do this:
if not myString:
This is the preferred way if you know that your variable is a string. If your variable could also be some other type then you should use
myString == "". See the documentation on Truth Value Testing for other values that are false in Boolean contexts.
Read more... Read less...
For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
So you should use:
if not some_string:
Just to clarify, sequences are evaluated to
True in a Boolean context if they are empty or not. They are not equal to
The most elegant way would probably be to simply check if its true or falsy, e.g.:
if not my_string:
However, you may want to strip white space because:
>>> bool("") False >>> bool(" ") True >>> bool(" ".strip()) False
You should probably be a bit more explicit in this however, unless you know for sure that this string has passed some kind of validation and is a string that can be tested this way.
This is what I would use to test if a string is either None OR Empty OR Blank:
def isBlank (myString): if myString and myString.strip(): #myString is not None AND myString is not empty or blank return False #myString is None OR myString is empty or blank return True
And, the exact opposite to test if a string is not None NOR Empty NOR Blank:
def isNotBlank (myString): if myString and myString.strip(): #myString is not None AND myString is not empty or blank return True #myString is None OR myString is empty or blank return False
More concise forms of the above code:
def isBlank (myString): return not (myString and myString.strip()) def isNotBlank (myString): return bool(myString and myString.strip())
def is_not_blank(s): return bool(s and s.strip())
print is_not_blank("") # False print is_not_blank(" ") # False print is_not_blank("ok") # True print is_not_blank(None) # False
The only really solid way of doing this is the following:
All other solutions have possible problems and edge cases where the check can fail.
len(myString)==0 can fail if
myString is an object of a class that inherits from
str and overrides the
myString == "" and
myString.__eq__("") can fail if
For some reason
"" == myString also gets fooled if
myString is "" and
"" is myString are equivalent. They will both fail if
myString is not actually a string but a subclass of string (both will return
False). Also, since they are identity checks, the only reason why they work is because Python uses String Pooling (also called String Internment) which uses the same instance of a string if it is interned (see here: Why does comparing strings using either '==' or 'is' sometimes produce a different result?). And
"" is interned from the start in CPython
The big problem with the identity check is that String Internment is (as far as I could find) that it is not standardised which strings are interned. That means, theoretically
"" is not necessary interned and that is implementation dependant.
The only way of doing this that really cannot be fooled is the one mentioned in the beginning:
"".__eq__(myString). Since this explicitly calls the
__eq__() method of the empty string it cannot be fooled by overriding any methods in myString and solidly works with subclasses of
Also relying on the falsyness of a string might not work if the object overrides it's
This is not only theoretical work but might actually be relevant in real usage since I have seen frameworks and libraries subclassing
str before and using
myString is "" might return a wrong output there.
Also, comparing strings using
is in general is a pretty evil trap since it will work correctly sometimes, but not at other times, since string pooling follows pretty strange rules.
That said, in most cases all of the mentioned solutions will work correctly. This is post is mostly academic work.