Check Empty String Python: 3 Unique Approaches

How can you check if a string is empty in Python?

To determine if a string is empty in Python use of the following three means available: see if the string has any length by using the built-in len() function, do a direct comparison using the expression == "" or convert the string to a boolean data type and if the result is false it is an empty string.

Let’s look at each approach and see how they perform.

Using len() Function

One way of being able to determine whether a string is empty is to use the len() function . The len() function only has one parameter which can be either a sequence (such as a string) or a collection (such as a dictionary or set) and this built-in function returns, in the case where the parameter entered is a string, the quantity of characters in the string.

Here is an example:

>>> my_string = "How long is a piece of string?"
>>> len(my_string)
30

As you can see from the above Python REPL example, by creating a variable my_string assigning that variable to the string value "How long is a piece of string?" and inserting my_string into the len() function the output shows the value 30 representing the total number of characters in the string – including the spaces and the question mark at the end.

Therefore, to determine if a string is empty you can use the len() function to see if the result is 0 :

>>> my_empty_string = ""
>>> len(my_empty_string)
0

As you can see from the empty string inserted into the len() function you get the value of 0 . Therefore, when determining if a string is empty you can use this function in your expression.

if len(my_empty_string) == 0:
    # my_empty_string is an empty string...
    print("Empty string!")

The only problem with this approach is that there are other data types within Python that can yield a result of 0 when using this function, here are some examples:

>>> len([])
0
>>> len({})
0
>>> len(())
0
>>> len(set())
0

As you can see from the above there are other results that produce a length of 0. You could further extend your expression by including a comparison of the variable’s data type, such as:

if len(my_empty_string) == 0 and type(my_empty_string) == str:
    print("Empty string")

The built-in function type() determines the class of the variable and if it is the str class then you will know the variable is a string.

Using == ""

Another approach when determining if a string is empty is to simply use a direct comparison. An empty string simply has nothing in it, therefore, it would look like this "" (nothing between the double apostrophes).

Knowing this would mean you could simply compare a variable or function’s returned value directly, as the following example shows:

my_empty_string = ""

if my_empty_string == "":
    # my_empty_string is an empty string...
    print("Empty string!")

As strings can be bounded by single apostrophes and double apostrophes it does not matter how your string is bounded it is still a string and can be compared to both different types as shown in the following REPL example:

>>> "" == ''
True
>>> '' == ""
True

Using the direct approach when determining if a string is empty is the easiest and cleanest way as it doesn’t involve any functions or additional data type checking and is also very clear to other users on what is being checked.

Empty Strings Are Falsey Values

Another approach to determining whether a value is an empty string is to have it be automatically converted to a boolean value when used in an expression. This approach is the most succinct, but may not be the clearest.

Here is an example using the Python REPL:

>>> my_empty_string = ""
>>> if not my_empty_string:
...     print("Empty string!")
...
Empty string

What happened here?

In the example above the variable my_empty_string was converted to a boolean data type. To know how Python would have handled an empty string as a boolean data type, just use the built-in bool() function, like so:

>>> bool(my_empty_string)
False

As you can see from the result of the bool() function using the empty string variable the result is False .

Therefore, when using just this variable in an expression if you want to capture when the variable is an empty string you would want to apply the inverse operator not , as something that is not False is True .

Conversely, if you want to proceed when the variable is not an empty string then you can remove the not operator from the if statement expression:

>>> my_string = "How long is a piece of string?"
>>> if my_string:
...     print("String not empty")
...
String not empty

Similarly, with the len() function approach this approach does have issues as some values can be Falsey. Here are some:

>>> bool(0)
False
>>> bool([])
False
>>> bool({})
False
>>> bool(())
False
>>> bool(set())
False
>>> bool(False)
False

As you can see from the list above there are a few more items than the len() function which produce False values. Therefore, you may need to include the type() check in your statements to ensure you’re operating on a string value.

Summary

To determine if something is an empty string use either the built-in len() function, the direct comparison approach == "" or the fact that empty strings are False values in your expressions.

The len() function and the Falsey value approach may cause issues if the variable could be something other than a string. If so, use the built-in function type() to check if the value is a str .

The direct approach helps keep your code succinct without the need to explicitly use the type() function and makes your code easily readable on what is being achieved.

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.