Python traps · Python

Python: bool("False") is TRUE?!FREE

Watch the video

bool() of a string only checks whether it is empty. Compare the text itself instead.

# bool("False") is True ?!
# bool() of a string only asks "is it empty?". "False" has 5 letters, so it's True.
print(bool("False"))   # True ?!
print(bool(""))        # False: only the empty string is False

# Fix: compare the text itself
s = "False"
print(s == "True")     # False ✓
Open code P6 in Pocket Compiler Download the Pocket Compiler app — free

No app yet? Install it, then open the app and type the code P6 in Code Library.