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:
#1 Best Overall
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.
Rank #2
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.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Quick Recap
Best Value
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.




