Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober 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 desk2 min

How to Check Whether a String Is Non-Empty in Python

Use len(s) > 0 for an explicit length check, if s for the concise non-empty test, and if s.strip() when whitespace-only input should count as blank.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use len(s) > 0 to explicitly check whether a string’s length is greater than zero. If s is known to be a string and you only need to know whether it is empty, the idiomatic shorthand is if s:.

Check whether the string has at least one character

To express the numeric comparison directly:

if len(s) > 0:
    print("The string is non-empty")

Python treats an empty string ('') as false in a condition and a non-empty string as true. So, for ordinary strings, if s: checks the same practical condition:

if s:
    print("The string is non-empty")

Use len(s) > 0 when the positive-length comparison itself is important to show. Use if s: when the intent is simply to test whether a string is non-empty. The behavior is documented in Python’s truth-value testing and built-in len() references.

Decide whether whitespace counts as empty

A string containing spaces, tabs, or other whitespace characters is still non-empty, so if s: passes for a value such as ' '. If whitespace-only input should count as blank, test the result of strip() instead:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
if s.strip():
    print("The string has non-whitespace content")

str.strip() removes whitespace from both ends and returns a new string; it does not change s. This checks for content after trimming, not whether the original string has a length greater than zero. See Python’s str.strip() documentation.

Account for missing values and other types

If s may be None or a value that is not a string, first decide how your application should handle it: validate that it is a string, reject it, or convert it. A plain truthiness check treats both None and '' as false, so it does not tell you which one you received.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Do not confuse non-empty with Boolean text

Truthiness tests whether a string contains characters; it does not interpret the characters as a Boolean value. For example, 'False' is non-empty and therefore truthy. If you need to interpret text as a true-or-false value, define and apply that parsing rule separately.

Also note that Python’s and and or operators can return one of their operands rather than a Boolean. That distinction usually does not matter in if s:, but it matters if you expect a compound expression itself to produce exactly True or False; not returns a Boolean.

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

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
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.