Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

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

Use `value is None` to detect only None. Use `if not value` only when None, False, zero, and empty values should all count as false.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use value is None to check whether a variable is specifically None. Use if not value when any false-valued value—including None, False, zero, or an empty container—should take the same path. These checks answer different questions.

Choose the check that matches what “empty” means

Check What it matches Use it when
value is None Only the None singleton None alone means missing or unset
not value None, False, numeric zero, empty built-in sequences and collections, and objects that define false truth behavior All false-valued inputs should be treated alike

The distinction is about meaning, not speed. Python defines None as a single object commonly used to represent the absence of a value, so test for it by identity: value is None. Python Built-in Types documentation and the language reference on identity comparisons describe these rules.

Check only for None

if value is None:
    handle_missing()

This leaves valid false-valued inputs alone. For example, if 0, False, '', [], or {} are meaningful values in your program, an explicit None check will not mistake them for missing data.

Prefer is None to == None. The former asks whether value is the same object as the None singleton; equality comparisons can follow an object’s custom equality behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check for any false-valued value

if not value:
    handle_empty_or_false_value()

In a condition, Python tests an object’s truth value. None, False, numeric zero, and empty built-in sequences and collections are false. By default, other objects are true unless their class defines __bool__() to return False or __len__() to return zero. See the official truth-value testing documentation.

That means if not value is broader than a check for an empty string or container. Use it only when the program should handle every false-valued case the same way.

Check for an empty string or blank text

Exactly an empty string

if value == "":
    handle_empty_string()

This tests for the empty string specifically; it does not classify None or numeric zero as an empty string.

Whitespace-only text

if value is not None and not value.strip():
    handle_blank_text()

A string containing spaces or other whitespace is nonempty, so it is truthy. Calling strip() makes a whitespace-only string count as blank. This expression assumes value is either None or a string; validate or convert other input types as your application requires.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use a fallback without discarding zero or False

This expression uses truthiness, so it selects fallback for any false-valued value:

result = value or fallback

If only None should trigger the fallback, write the condition explicitly:

result = fallback if value is None else value

Python’s or operator returns one of its operands: it evaluates the left operand’s truth value and returns the fallback when that operand is false. The language reference on Boolean operations specifies this behavior.

Common mistakes to avoid

  • Treating “false” as synonymous with “not provided.” if not value also catches zero, False, and empty values. Use value is None when those may be valid.
  • Using equality for a None check. Write value is None to test the singleton directly.
  • Assuming custom objects use ordinary emptiness rules. A class can control its truth value through __bool__() or __len__().
  • Expecting whitespace to be an empty string. Whitespace makes a string nonempty; strip it explicitly if your rule treats blank text as empty.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.