Skip to main content

How to Check if a String Is Empty or None in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A value your script needs, such as a setting or a field from a form, can be missing as an empty string "" or as None. Python treats both as false in a condition, so a single if not value: catches either.

Syntax​

Syntax
if not <value>:
# <value> is "", None or another false value
PartWhat it does
not <value>True when <value> counts as false, such as "" or None.
if ...:Runs the indented block when the test is True.

The Python documentation on truth value testing lists the built-in objects considered false, among them "constants defined to be false: None and False" and "empty sequences and collections: '', (), [], {}, set(), range(0)".

The Example​

The example runs three values through one check: a name, an empty string and None.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Check​

Create a file named main.py:

main.py, which defines describe(value) returning empty or missing when not value is true and has text: followed by the value otherwise, and prints it for Ada, an empty string and None.

main.py
def describe(value):
if not value:
return "empty or missing"
return f"has text: {value}"


print(describe("Ada"))
print(describe(""))
print(describe(None))

Step 2: Run It​

Terminal
uv run python main.py
Output
has text: Ada
empty or missing
empty or missing

A zsh terminal where uv run python main.py prints has text: Ada and then empty or missing twice.

not value was True for both "" and None, so one test handled both. The command is the same in zsh, bash and PowerShell.

What About Spaces?​

A string of spaces isn't empty, so it counts as true and passes the check: bool(" ") is True. When spaces should count as missing, strip them first, and test for None before calling a string method:

Blank strings count as missing
if value is None or not value.strip():
...

value is None is tested first, so value.strip() only runs on a real string.

Common Mistakes​

MistakeWhat happensDo this instead
Calling value.strip() when value may be NoneAttributeError: 'NoneType' object has no attribute 'strip'Test value is None first
Using if not value for numbers0 is false too, so a real 0 counts as missingTest numbers with value is None
Expecting " " to count as emptyIt's true, because it isn't emptyCheck not value.strip()

Troubleshooting​

SymptomLikely causeFix
A blank field passes the checkIt holds spacesStrip before testing
AttributeError: 'NoneType' object has no attribute …A string method ran on NoneCheck for None before calling it

FAQ​

Is the string "0" false?​

No. Only the empty string is a false string; "0", "False" and " " are all true.

How do I check for None only?​

Use value is None. It's True only for None, not for "".

Conclusion​

if not value: catches both "" and None in one test, because both are false. Strip the string first when blank strings should count as missing too.

Additional Resources​