How to check if the string is empty in Python?

Asked 2023-09-20 20:58:03 View 934,679

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.

  • How is "" not as good? - anyone
  • None won't match "". So depends on the context as to what you want. - anyone

Answers

Empty strings are "falsy" (python 2 or python 3 reference), 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:

if myString == "":

See the documentation on Truth Value Testing for other values that are false in Boolean contexts.

Answered   2023-09-20 20:58:03

  • Be careful, since lots of other things are false as well. - anyone
  • I never heard of the term falsy before. Does it mean it returns false? - anyone
  • @Joan: It evaluates as false in a boolean context. - anyone
  • OP wants to know if the variable is an empty string, but you would also enter the if not myString: block if myString were None, 0, False etc. So if you aren't sure what type myString is, you should use if myString == "": to determine if it is an empty string as opposed to some other falsy value. - anyone
  • @AndrewClark, for such a case, instead of a chain of if myString == ... expressions, we could use if myString in (None, '') or per @Bartek, if myString in (None, '') or not myString.strip() - anyone

From PEP 8, in the “Programming Recommendations” section:

For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

So you should use:

if not some_string:

or:

if some_string:

Just to clarify, sequences are evaluated to False or True in a Boolean context if they are empty or not. They are not equal to False or True.

Answered   2023-09-20 20:58:03

  • PS: In the PEP's defense, one could argue that saying "x is false" (lowercase false) already means that, rather than meaning x == False. But IMHO the clarification is still welcome given the target audience. - anyone
  • if not myString catch as empty this var='' in python 3.8 - anyone

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.

Answered   2023-09-20 20:58:03

I would test noneness before stripping. Also, I would use the fact that empty strings are False (or Falsy). This approach is similar to Apache's StringUtils.isBlank or Guava's Strings.isNullOrEmpty

This is what I would use to test if a string is either None OR empty OR blank:

def isBlank (myString):
    return not (myString and myString.strip())

And the exact opposite to test if a string is not None NOR empty NOR blank:

def isNotBlank (myString):
    return bool(myString and myString.strip())

Answered   2023-09-20 20:58:03

  • why not if mystring and not mystring.strip()? - anyone
  • How is it different from string and not string.isspace()? - anyone
  • More concise for those who care about such things: def isBlank(s): return not (s and s.strip()) and def isNotBlank(s): return s and s.strip(). - anyone
  • s.strip() allocates a new string, which is pure waste. Use string.isspace() - anyone
  • this answer is not very pythonic - anyone

I once wrote something similar to Bartek's answer and javascript inspired:

def is_not_blank(s):
    return bool(s and not s.isspace())

Test:

print is_not_blank("")    # False
print is_not_blank("   ") # False
print is_not_blank("ok")  # True
print is_not_blank(None)  # False

Answered   2023-09-20 20:58:03

  • Why not just return bool(s.strip()) - anyone
  • AttributeError: 'NoneType' object has no attribute 'strip' - anyone
  • s.strip() allocates a new string, which is pure waste. Use string.isspace() - anyone
  • "".isspace() is False - anyone
  • @Danon is will not pass the first test s and and will short-circuit. - anyone

The only really solid way of doing this is the following:

if "".__eq__(myString):

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 __len__() method.

  • myString == "" and myString.__eq__("") can fail if myString overrides __eq__() and __ne__().

  • "" == myString also gets fooled if myString overrides __eq__().

  • 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.

    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.

  • Relying on the falsyness of a string may not work if the object overrides __bool__().

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 str.

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.

That said, in most cases all of the mentioned solutions will work correctly. This is post is mostly academic work.

Answered   2023-09-20 20:58:03

  • @simpleuser you are of course right. In the real world it is completely ok to use the falsiness of the string or compare it to an empty string. This here answer is overkill on purpose. - anyone
  • pylint does complains about your solution : xx.py:68:14: C2801: Unnecessarily calls dunder method eq. Use == operator. (unnecessary-dunder-call) - anyone
  • @BrunoRohée It would have been weird if pylint didn't actually complain^^ But in this specific case it's not correct, since == for some reason would call the overridden version as explained above. At least, 4 years ago that was correct. Might have changed since. But all in all, this post was more of an elaborate joke and I hope nobody actually used it in production code. - anyone

Test empty or blank string (shorter way):

if myString.strip():
    print("it's not an empty or blank string")
else:
    print("it's an empty or blank string")

Answered   2023-09-20 20:58:03

  • If myString = None, it will raise an exception. Better use @vault's answer - anyone
  • @Dominik if myString isinstance(myString, int) will also raise exception. What is your point? The OP asked for ways to test empty strings, his question does not include type checking. - anyone

If you want to differentiate between empty and null strings, I would suggest using if len(string), otherwise, I'd suggest using simply if string as others have said. The caveat about strings full of whitespace still applies though, so don't forget to strip.

Answered   2023-09-20 20:58:03

  • I don't know why you'd want to avoid using "", unless it impacts performance somehow, but I prefer your answer to the one with zillions of upvotes, as it's less confusing. However, I wanted to point out that an empty list is also False, apparently. - anyone

if stringname: gives a false when the string is empty. I guess it can't be simpler than this.

Answered   2023-09-20 20:58:03

I find hardcoding(sic) "" every time for checking an empty string not as good.

Clean code approach

Doing this: foo == "" is very bad practice. "" is a magical value. You should never check against magical values (more commonly known as magical numbers)

What you should do is compare to a descriptive variable name.

Descriptive variable names

One may think that "empty_string" is a descriptive variable name. It isn't.

Before you go and do empty_string = "" and think you have a great variable name to compare to. This is not what "descriptive variable name" means.

A good descriptive variable name is based on its context. You have to think about what the empty string is.

  • Where does it come from.
  • Why is it there.
  • Why do you need to check for it.

Simple form field example

You are building a form where a user can enter values. You want to check if the user wrote something or not.

A good variable name may be not_filled_in

This makes the code very readable

if formfields.name == not_filled_in:
    raise ValueError("We need your name")

Thorough CSV parsing example

You are parsing CSV files and want the empty string to be parsed as None

(Since CSV is entirely text based, it cannot represent None without using predefined keywords)

A good variable name may be CSV_NONE

This makes the code easy to change and adapt if you have a new CSV file that represents None with another string than ""

if csvfield == CSV_NONE:
    csvfield = None

There are no questions about if this piece of code is correct. It is pretty clear that it does what it should do.

Compare this to

if csvfield == EMPTY_STRING:
    csvfield = None

The first question here is, Why does the empty string deserve special treatment?

This would tell future coders that an empty string should always be considered as None.

This is because it mixes business logic (What CSV value should be None) with code implementation (What are we actually comparing to)

There needs to be a separation of concern between the two.

Answered   2023-09-20 20:58:03

  • Does one really need to go to that much trouble to avoid a ""? In the context of comparison what else could a blank string mean? - anyone
  • you say "not_filled_in" is more descriptive than "empty_string" ? I say you are high. - anyone
  • I would disagree with that answer. Magic numbers are bad, that makes sense. Magic values in general as well. But "" is not a magic value, same as True, False or None aren't magic values. - anyone
  • @firelynx how much more explicit explicit can you get to say "this string is empty" than to use ""? Would you also redefine True as REALLY_TRUE? In the end you will end up with a lot of variables (that are non-constant compared to a literal "") that say the same thing. So, is CSV_NONE different to JSON_EMPTY_VALUE? The problem then occurs when you transfer between different parts of the program. Then you might not know anymore if you have a None or a "", especially if you name your constants something misleading like CSV_NONE, which is actually not None but "". - anyone
  • @firelynx This causes more ambiguity than just use the constant values None, True, False or "". Using variables instead of magic values is useful whenever you encode more information in the name then what the value itself already has, e.g. use STATE_A instead of 27. Or whenever there is ambiguity, e.g. GPIO.HIGH instead of 1, because depending on the logic you use, 1 could be HIGH or LOW. But in the given example using a variable causes more harm than good. - anyone

How about this? Perhaps it's not "the most elegant", but it seems pretty complete and clear:

if (s is None) or (str(s).strip()==""): // STRING s IS "EMPTY"...

Answered   2023-09-20 20:58:03

  • For most purposes, a string containing blanks is not "empty". - anyone
  • I assume you mean nothing but white space? You are referring to my use of strip()? For most purposes that IS empty! It is ridiculously common to compound something like s.trim().isEmpty() - anyone
  • Hey @Chris Johnson, did you see that MOST of the answers here use strip() as well? Did you down vote all of us, or just me? - anyone

if you want to check if a string is completely empty

if not mystring

which works because empty strings are false but if a string is only whitespace it will be true so you might want to

if not mystring.strip()

Answered   2023-09-20 20:58:03

  • Isn't this already covered by the other answers? - anyone
  • @MateenUlhaq Sorry for the late response,but Yes you are right but I feel that more answers saying the same thing gives the op more reassurance,and to be totally honest I don't really care about stupid internet points. - anyone

I did some experimentation with strings like '', ' ', '\n', etc. I want isNotWhitespace to be True if and only if the variable foo is a string with at least one non-whitespace character. I'm using Python 3.6. Here's what I ended up with:

isWhitespace = str is type(foo) and not foo.strip()
isNotWhitespace = str is type(foo) and not not foo.strip()

Wrap this in a method definition if desired.

Answered   2023-09-20 20:58:03

Responding to @1290. Sorry, no way to format blocks in comments. The None value is not an empty string in Python, and neither is (spaces). The answer from Andrew Clark is the correct one: if not myString. The answer from @rouble is application-specific and does not answer the OP's question. You will get in trouble if you adopt a peculiar definition of what is a "blank" string. In particular, the standard behavior is that str(None) produces 'None', a non-blank string.

However if you must treat None and (spaces) as "blank" strings, here is a better way:

class weirdstr(str):
    def __new__(cls, content):
        return str.__new__(cls, content if content is not None else '')
    def __nonzero__(self):
        return bool(self.strip())

Examples:

>>> normal = weirdstr('word')
>>> print normal, bool(normal)
word True

>>> spaces = weirdstr('   ')
>>> print spaces, bool(spaces)
    False

>>> blank = weirdstr('')
>>> print blank, bool(blank)
 False

>>> none = weirdstr(None)
>>> print none, bool(none)
 False

>>> if not spaces:
...     print 'This is a so-called blank string'
... 
This is a so-called blank string

Meets the @rouble requirements while not breaking the expected bool behavior of strings.

Answered   2023-09-20 20:58:03

  • python -c "if (str(None) == 'None'): print ('OMG, WHY ??')" - anyone
a = ''
b = '   '
a.isspace() -> False
b.isspace() -> True

Answered   2023-09-20 20:58:03

  • Really don't understand what this solution gains. The question is about testing whether a string is empty. If you set a='a' you would get a.isspace() -> False, but a would not on that account be an empty string. - anyone

The clearest approach is:

if s == "":

Benefits:

  • Additional indication to the programmer what the type of s should be.
  • "" is not "hard-coding" a magic value any more than x == 0 is. Some values are fundamental and do not need a named constant; e.g. x % 2 to check for even numbers.
  • Cannot incorrectly indicate that any falsy value (e.g. []) is an empty string.

Consider how one checks if an integer is 0:

if x == 0:

One certainly should not do:

if not x:

Both integers and strings are primitive value types. Why treat them differently?

Answered   2023-09-20 20:58:03

not str(myString)

This expression is True for strings that are empty. Non-empty strings, None and non-string objects will all produce False, with the caveat that objects may override __str__ to thwart this logic by returning a falsy value.

Answered   2023-09-20 20:58:03

for those who expect a behaviour like the apache StringUtils.isBlank or Guava Strings.isNullOrEmpty :

if mystring and mystring.strip():
    print "not blank string"
else:
    print "blank string"

Answered   2023-09-20 20:58:03

When you are reading file by lines and want to determine, which line is empty, make sure you will use .strip(), because there is new line character in "empty" line:

lines = open("my_file.log", "r").readlines()

for line in lines:
    if not line.strip():
        continue

    # your code for non-empty lines

Answered   2023-09-20 20:58:03

If you are not totally sure, that your input is really a string, I would recommend to use isinstance(object, classinfo) link in addition, as shown in the example.

If not, lists or a True bool value could also be evaluated as True.

<script type="text/javascript" src="//cdn.datacamp.com/dcl-react.js.gz"></script>

<div data-datacamp-exercise data-lang="python">
  <code data-type="sample-code">
def test_string(my_string):
    if isinstance(my_string, str) and my_string:
      print("It's a me, String! -> " + my_string)
    else: 
      print("Nope. No, String")

def not_fully_test_string(my_string):
    if my_string:
      print("It's a me, String??? -> " + str(my_string))
    else: 
      print("Nope. No, String")
  
print("Testing String:")
test_string("")
test_string(True)
test_string(["string1", "string2"])
test_string("My String")
test_string(" ")

print("\nTesting String or not?")
not_fully_test_string("")
not_fully_test_string(True)
not_fully_test_string(["string1", "string2"])
not_fully_test_string("My String")
not_fully_test_string(" ")

  </code>
</div>

Answered   2023-09-20 20:58:03

You can check if the string is completely empty or not as shown below. *strip() can remove spaces:

test1 = ""
test2 = "    "
test3 = "    ".strip()

print(bool(test1), bool(test2), bool(test3))
         # False      # True       # False

if test1:
    print("test1") # Not run
    
if test2:
    print("test2") # Run

if test3:
    print("test3") # Not run

if not test1:
    print("test1") # Run
    
if not test2:
    print("test2") # Not run

if not test3:
    print("test3") # Run

Answered   2023-09-20 20:58:03

You may have a look at this Assigning empty value or string in Python

This is about comparing strings that are empty. So instead of testing for emptiness with not, you may test is your string is equal to empty string with "" the empty string...

Answered   2023-09-20 20:58:03

If you just use

not var1 

it is not possible to difference a variable which is boolean False from an empty string '':

var1 = ''
not var1
> True

var1 = False
not var1
> True

However, if you add a simple condition to your script, the difference is made:

var1  = False
not var1 and var1 != ''
> True

var1 = ''
not var1 and var1 != ''
> False

Answered   2023-09-20 20:58:03

In case this is useful to someone, here is a quick function i built out to replace blank strings with N/A's in lists of lists (python 2).

y = [["1","2",""],["1","4",""]]

def replace_blank_strings_in_lists_of_lists(list_of_lists):
    new_list = []
    for one_list in list_of_lists:
        new_one_list = []
        for element in one_list:
            if element:
                new_one_list.append(element)
            else:
                new_one_list.append("N/A")
        new_list.append(new_one_list)
    return new_list


x= replace_blank_strings_in_lists_of_lists(y)
print x

This is useful for posting lists of lists to a mysql database that does not accept blanks for certain fields (fields marked as NN in schema. in my case, this was due to a composite primary key).

Answered   2023-09-20 20:58:03

Below is an elegant solution for any number of spaces.

def str_empty(s: str) -> bool:
    """Strip white space and count remaining characters."""
    return len(s.strip()) < 1


>>> str_empty(' ')
True

Answered   2023-09-20 20:58:03

  • .strip() isn't directly relevant to question. Also, creating a str_empty function for something as simple as s == "" is way too much. Also, len(s) == 0 would be a nicer expression than len(s) < 1. - anyone
  • None of these prevent the solution from working, as a matter of preference. Meanwhile, your preferred solution does not work as s == "" and s == " " are not equivalent, yet they can both be considered empty for most purposes. - anyone
  • If dealing with whitespace is the goal, any solution that uses .strip() before checking if the resulting string is empty works. s.strip() == ""; not s.strip(); len(s.strip()) == 0; ... However, whitespace wasn't really the point of this question, so I find it unusual that so many answers talk about it. - anyone
  • That is because 'empty' is often interpreted as either truly empty or consisting of whitespace. Hence, a solution that works for both seems appropriate. - anyone

As prmatta posted above, but with mistake.

def isNoneOrEmptyOrBlankString (myString):
    if myString:
        if not myString.strip():
            return True
        else:
            return False
    return False

Answered   2023-09-20 20:58:03

  • Just tested: His code returns True for "" and " " and False for "a" (just as expected). Your code returns the same, except for the empty string it returns True, what it shouldn't. - anyone
  • sry too tired: Your code returns False for the empty string. - anyone
  • This code is wrong. You return False if a string is empty or none. - anyone