Python 3 reference · Built-in Functions
Python bool() Function
Convert a value to True or False using Python's truth rules: zero, empty and None are False, everything else is True.
Python 3 Since 3.0
Syntax
bool(x=False)
Parameters
| Parameter | Description |
|---|---|
x optional |
Any value. Default: False |
Return value
True or False.
Examples
print(bool(0), bool(42))
print(bool(""), bool("hi"))
print(bool([]), bool([0]))
print(bool(None))
Output
False True False True False True False
print(bool("False")) # a non-empty string is True!
print("False".lower() == "true")
Output
True False
print(True + True) # bool is a subclass of int
print(isinstance(True, int))
Output
2 True
Common mistakes
bool("False")andbool("0")areTrue: any non-empty string is truthy. Compare the text instead.- You rarely need
bool()in conditions:if items:already tests truthiness.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
| Python 3.7 | x became a positional-only parameter. |
Related
all()· Return True if every item in an iterable is truthy, or if the iterable is empty. Stops at the first falsy item.any()· Return True if at least one item in an iterable is truthy. Returns False for an empty iterable and stops at the first truthy item.int()· Convert a number or string to an integer. Floats are truncated toward zero, and strings can be in any base from 2 to 36.- Python Booleans · tutorial