DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Check if a String Is Empty in Python: Empty, Blank, None, or NaN

Use `not s` for an exactly empty Python string and `not s.strip()` when whitespace-only strings should count as blank. Check None and NaN separately.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a value you know is a Python string, use not value to check whether it is exactly empty, or not value.strip() to treat whitespace-only strings as blank. None and NaN are different kinds of values, so handle them with their own checks.

Check whether a string is exactly empty

An empty string has zero characters. Python strings are false-valued when empty, so a concise check is:

value = ""

if not value:
    print("empty string")

This works when value is known to be a string. It does not consider spaces or tabs empty: " " contains characters and is truth-valued.

Python’s truth-value rules describe which objects evaluate as false, including empty strings.

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

Check whether a string is blank or whitespace-only

If strings containing only surrounding whitespace should count as blank, call strip() before testing:

value = "   tn"

if not value.strip():
    print("empty or whitespace-only string")

str.strip() returns a copy with leading and trailing whitespace removed. If nothing remains, the string is empty or whitespace-only under that method’s definition. See Python’s string method documentation.

Handle None separately

None is not a string; it is Python’s singleton object for the absence of a value. Check it with identity comparison, then check strings only when appropriate:

if value is None:
    print("missing value")
elif isinstance(value, str) and not value.strip():
    print("empty or blank string")

The is None check avoids confusing a missing value with an empty string. Python documents None as a singleton constant in its built-in constants reference.

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

Check NaN with a NaN predicate

NaN (“not a number”) is a floating-point value, not a string. Equality is not a reliable way to detect it: NaN compares unequal to itself, so a comparison with float("nan") will not identify it. For a compatible numeric scalar, use math.isnan():

import math

value = float("nan")
if math.isnan(value):
    print("NaN")

For NumPy numeric values, use numpy.isnan() (commonly imported as np.isnan()). NumPy documents the behavior of IEEE 754 special floating-point values and its isnan function.

Choose a check for the input you actually have

Input and meaning Use What the check returns or means
Known string; only zero characters count not value True for "", false for whitespace-only strings.
Known string; whitespace-only also counts as blank not value.strip() True when stripping whitespace leaves no characters.
Value may be absent value is None Checks specifically for Python’s None singleton.
Compatible numeric scalar may be NaN math.isnan(value) Checks for NaN; requires a compatible numeric value.
NumPy value or array np.isnan(value) Checks NumPy numeric values; array input produces array-like results.
Pandas-supported scalar or tabular data may be missing pd.isna(value) Recognizes missing values such as None, NaN, and NaT. Scalar input yields a scalar result; array-like input yields array-like booleans.

For mixed or tabular data already handled with pandas, pd.isna() is the library-level missing-value check:

import pandas as pd

pd.isna(None)       # True
pd.isna(float("nan"))  # True

For a Series or DataFrame, pd.isna() returns corresponding array-like booleans, not one ordinary Boolean. Do not use that result directly in a single if condition; reduce it according to the question you mean to ask, such as whether any entries are missing or all entries are missing. See the pandas.isna documentation.

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

Avoid the wrong generic check

Do not call .strip() on an unknown value unless you have established it is a string or chosen what to do with other types. A number, None, or another object may not have that method.

Likewise, if not value: checks general truthiness, not specifically whether a value is a missing string. It also treats values such as numeric zero, False, and empty containers as false. Use it for a string only when the input is actually a string; otherwise use a type-appropriate check.

Quick decision rule

  • Known string, exact emptiness: not s.
  • Known string, blank includes whitespace-only: not s.strip().
  • Possible missing Python value: check s is None.
  • Possible numeric NaN: use math.isnan(), np.isnan(), or, for pandas-supported missing values, pd.isna().
  • Array or Series: account for the array-like result instead of expecting one Boolean.

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.

Leave a Reply

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

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
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

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.