Python Truthiness and bool()
Core idea
Truthiness is how Python interprets an object in a logical test. bool(value) returns the corresponding Boolean. Zero numbers and empty strings are false in such tests; nonzero ordinary numbers and nonempty strings are true. None, a special value used to represent absence, is false too.
Negative numbers are not automatically false: -3 is nonzero.
Check text
Only the first string is empty. The other strings each contain one character, even though one looks like zero and one looks blank.
Distinguish absence from zero
value = Noneprint(bool(value))print(bool(0))print(value == 0)Both absence and zero are false in a truth test, but they are not equal. Empty collections such as [] (a list with no items) are also false.
Common mistake
bool("False") is True because the text is nonempty. Converting text with bool is not the same as parsing words such as yes or no.
Practice
Predict bool(" ") and bool(" ".strip()). Run both and explain how trimming changes the test.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Truthiness
Check your understanding of truthiness.
What is printed?
print(bool(-4))What is printed?
print(bool("0"))What is printed?
print(bool(" ".strip()))Which value is false in a truth test?