
John L. answered 05/24/19
Machine Learning Professor and More
All Python types have the ability to be evaluated for "truthiness," which is returned by the built-in Boolean conversion function, bool().
Someone had to decide what the truthiness property is for each object type. Python's developers chose to make the truthiness property of strings a function of their length. Taking bool() on a zero-length string evaluates to False. The empty string is the only possible zero-length string. All other strings return True.
An if statement in Python must have a Boolean argument. So you don't have to write "if bool(myString)". "if myString" is syntactic sugar for calling bool(myString).
Also, you don't have to explicitly write "True" or "False" if you already have the Boolean argument. You can either just state the variable name if you are testing for True, and use the "not" operation if you are testing for False.
So the most EXPLICIT way to write your test in Python might be:
if bool(myString) == False:
But once you are comfortable with the shortcuts, the most ELEGANT way to write your test is:
if not myString: