Convert Between bool (True/False) and Other Types in Python
In Python, Boolean values (truth values) are represented by the bool
type objects True
and False
. This article explains how to convert between bool
and other types in Python.
bool
is a Subclass of int
Both True
and False
are instances of the bool
type:
print(type(True))
# <class 'bool'>
print(type(False))
# <class 'bool'>
Moreover, bool
is a subclass of int
:
print(issubclass(bool, int))
# True
True
and False
Are Equivalent to 1
and 0
In Python, True
is equivalent to 1
and False
is equivalent to 0
:
print(True == 1)
# True
print(False == 0)
# True
Because bool
is a subclass of int
, Boolean values can be used in arithmetic operations:
print(True + True)
# 2
print(True * 10)
# 10
This means you can count the number of True
values in a list using the built-in sum()
function:
print(sum([True, False, True]))
# 2
You can also use a generator expression to count the number of elements that meet a condition. A generator expression is similar to a list comprehension, but uses parentheses ()
instead of square brackets []
.
If the generator expression is the only argument to a function, you can omit the parentheses:
l = [0, 1, 2, 3, 4]
print([i > 2 for i in l])
# [False, False, False, True, True]
print(sum(i > 2 for i in l))
# 2
Truth Value Testing in Python
In Python, objects other than True
and False
can also evaluate as either true or false in conditionals such as if
statements.
The following are considered falsy:
- constants defined to be false:
None
andFalse
- zero of any numeric type:
0
,0.0
,0j
,Decimal(0)
,Fraction(0, 1)
- empty sequences and collections:
''
,()
,[]
,{}
,set()
,range(0)
Built-in Types - Truth Value Testing — Python 3.13.3 documentation
All other objects are considered truthy. For example, a non-empty string is truthy:
if 'abc':
print('True!')
# True!
Convert Other Types to bool
: bool()
bool()
evaluates an object according to Python’s rules for truth value testing and returns either True
or False
.
Any non-empty string, including 'True'
and 'False'
, is treated as True
. Only an empty string becomes False
. To convert a string based on its content, use strtobool()
(explained below):
print(bool('True'))
print(bool('False'))
print(bool('abc'))
# True
# True
# True
print(bool(''))
# False
Non-zero numbers evaluate to True
; zero values evaluate to False
:
print(bool(1))
print(bool(2))
print(bool(1.23))
print(bool(-1))
# True
# True
# True
# True
print(bool(0))
print(bool(0.0))
print(bool(0j))
# False
# False
# False
Non-empty sequences and collections are True
; empty ones are False
:
print(bool([1, 2, 3]))
print(bool((1, 2, 3)))
print(bool({1, 2, 3}))
print(bool({'k1': 1, 'k2':2, 'k3': 3}))
# True
# True
# True
# True
print(bool([]))
print(bool(()))
print(bool({}))
# False
# False
# False
The special constant None
is always evaluated as False
.
print(bool(None))
# False
Convert Strings to bool
Based on Content: strtobool()
As mentioned above, bool('False')
still returns True
because it's a non-empty string. To convert strings based on their content, use strtobool()
.
Up to Python 3.11, strtobool()
was included in the standard library's distutils.util
module. In Python 3.12 and later, since distutils
has been removed, it is available via the setuptools
package, which is not part of the standard library and must be installed separately (e.g., using pip).
# For Python 3.11 and earlier
from distutils.util import strtobool
# For Python 3.12 and later (requires installing setuptools)
from setuptools._distutils.util import strtobool
The following case-insensitive strings are considered valid inputs:
- Truthy:
'y'
,'yes'
,'t'
,'true'
,'on'
,'1'
- Falsy:
'n'
,'no'
,'f'
,'false'
,'off'
,'0'
Any other input will result in a ValueError
.
True values are
y
,yes
,t
,true
,on
and1
; false values aren
,no
,f
,false
,off
and0
. RaisesValueError
ifval
is anything else. 9. API Reference - distutils.util.strtobool(val) — Python 3.11.12 documentation
Previously, strtobool()
returned 1
or 0
(as integers), but starting with setuptools
version 75.3.2 (released on 2025-03-12), it returns True
or False
.
from setuptools._distutils.util import strtobool
print(strtobool('true'))
print(strtobool('True'))
print(strtobool('TRUE'))
# True
# True
# True
print(strtobool('t'))
print(strtobool('yes'))
print(strtobool('y'))
print(strtobool('on'))
print(strtobool('1'))
# True
# True
# True
# True
# True
print(strtobool('false'))
print(strtobool('False'))
print(strtobool('FALSE'))
# False
# False
# False
print(strtobool('f'))
print(strtobool('no'))
print(strtobool('n'))
print(strtobool('off'))
print(strtobool('0'))
# False
# False
# False
# False
# False
Invalid inputs raise a ValueError
:
# print(strtobool('abc'))
# ValueError: invalid truth value 'abc'
To support non-standard inputs, use a try-except block:
try:
strtobool('abc')
except ValueError as e:
print(e)
# invalid truth value 'abc'
If you want to support additional input strings, you can define your own version of strtobool()
based on the implementation in setuptools
.
def strtobool(val: str) -> bool:
"""Convert a string representation of truth to true (1) or false (0).
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return False
else:
raise ValueError(f"invalid truth value {val!r}")
It uses lower()
to normalize case and checks membership using the in
operator.
- Uppercase and lowercase strings in Python (conversion and checking)
- The in operator in Python (for list, string, dictionary, etc.)
Convert bool
to Other Types
To numbers: int()
, float()
, complex()
Since True
and False
are equivalent to 1
and 0
, they can be converted to numeric types:
print(int(True))
print(int(False))
# 1
# 0
print(float(True))
print(float(False))
# 1.0
# 0.0
print(complex(True))
print(complex(False))
# (1+0j)
# 0j
To String: str()
Use str()
to convert True
and False
to the strings 'True'
and 'False'
:
print(str(True))
print(str(False))
# True
# False
print(type(str(True)))
print(type(str(False)))
# <class 'str'>
# <class 'str'>
Keep in mind that all non-empty strings are truthy, so converting False
to a string and back to bool
results in True
:
print(bool(str(False)))
# True
To Other Types
You cannot directly convert True
or False
into a list or tuple. Doing so raises a TypeError
because bool
objects are not iterable:
# print(list(True))
# TypeError: 'bool' object is not iterable