Built-in function

bool()

Tells you whether Python treats a value as true or false: empty things and zero are False, almost everything else is True.

BeginnerEvery example below runs in your browser. Change the code and press Run.

bool(x=False)

bool() turns any value into True or False. This is the same test Python does silently in every if and while: when you write if items:, Python checks bool(items).

The rule is simple. These are false: False, None, the number zero (0, 0.0), and anything empty ("", [], {}, (), set(), range(0)). Everything else is true.

The big surprise: bool("False") is True. Any string with at least one character is true, whatever it says, so bool() cannot read the words "yes" or "no". Compare the text yourself instead. Also, True and False are really the numbers 1 and 0, which makes counting easy.

Parameters

ParameterWhat it is
x
optional, default False
Any value. With nothing, bool() gives False.

Return value

True or False.

Examples

1Which values are false?

Pythoneditable
for value in [0, 0.0, "", [], {}, None, 7, -1, "0", " ", [0]]:
    print(repr(value), bool(value))
Output
0 False
0.0 False
'' False
[] False
{} False
None False
7 True
-1 True
'0' True
' ' True
[0] True

"0", a single space and a list holding 0 are all true: they are not empty.

2Check for empty input in an if

Pythoneditable
for name in ["Ines", ""]:
    if name:
        print("Hello,", name)
    else:
        print("No name given")
Output
Hello, Ines
No name given

if name: already calls bool(name) for you, so you rarely need to write bool() itself.

3Turn yes/no text into True/False

Pythoneditable
def to_bool(text):
    return text.strip().lower() in ("yes", "y", "true", "1")

print(bool("no"))
print(to_bool("no"))
print(to_bool(" Yes"))
Output
True
False
True

bool("no") is True because the string is not empty. A small helper that compares the words gives the answer you meant.

4Count with True and False

Pythoneditable
scores = [55, 81, 49, 92, 67]
passed = [s >= 60 for s in scores]
print(passed)
print(sum(passed), "of", len(scores), "passed")
print(True + True)
Output
[False, True, False, True, True]
3 of 5 passed
2

True behaves like 1 and False like 0, so sum() counts the True values.

Common mistakes

Expecting bool("False") to be False

Only the empty string is false. To read settings from a file or form, compare the text.

Pythoneditable
setting = "False"
print(bool(setting))
print(setting.lower() == "true")
Output
True
False

Writing if x == True

if logged_in == True: works but is longer than needed, and if count == True: is wrong when count is 5 (5 is not equal to True, which is 1). Write if logged_in: and if count:.

Pythoneditable
count = 5
print(count == True)
print(bool(count))
Output
False
True

Questions people ask

How do I convert a string to a boolean in Python?

Compare it: value.strip().lower() in ("true", "yes", "1"). bool(value) only checks whether the string is empty.

Is None the same as False?

No. Both count as false in an if, but None == False is False. To check for None specifically, write if x is None:.

Why is True equal to 1?

bool is a kind of int: True is 1 and False is 0. That is why sum() of booleans counts the true ones, and why isinstance(True, int) is True.

Spotted a mistake on this page? Tell us