Last updated: Apr 9, 2024
Reading timeยท3 min
Use the print()
function to print a boolean value in Python, e.g.
print(my_bool)
.
If the value is not of type boolean, use the bool()
class to convert it to a
boolean and print the result, e.g. bool(0)
.
my_bool = True # โ print boolean value print(my_bool) # ๐๏ธ True # โ print boolean value in a string print(f'is subscribed: {my_bool}') # ๐๏ธ is subscribed: True # โ print integer representation of boolean value print(int(True)) # ๐๏ธ 1 print(int(False)) # ๐๏ธ 0 # โ print boolean value converted to lowercase print(f'{my_bool}'.lower()) # ๐๏ธ true # โ Convert a value to a boolean print(bool('hello')) # ๐๏ธ True print(bool(0)) # ๐๏ธ False
We used the print()
function to print boolean values.
The print() function takes one or more
objects and prints them to sys.stdout
.
If you have a boolean value, you can pass it directly to the print()
function
to print it.
my_bool = True print(my_bool) # ๐๏ธ True print(False) # ๐๏ธ False
print()
function returns None
, so don't try to store the result of calling print
in a variable.my_bool = True # โ๏ธ BAD (print always returns None) result = print(f'is subscribed: {my_bool}') print(result) # ๐๏ธ None
Instead, store the value in a variable and pass the variable to the print()
function.
my_bool = True result = f'is subscribed: {my_bool}' print(result) # ๐๏ธ is subscribed: True
If you aren't sure what type a variable stores, use the built-in type()
class.
my_bool = False print(type(my_bool)) # ๐๏ธ <class 'bool'> print(isinstance(my_bool, bool)) # ๐๏ธ True my_str = 'hello' print(type(my_str)) # ๐๏ธ <class 'str'> print(isinstance(my_str, str)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.
If you need to convert a value to a boolean and print the result, use the bool() class.
print(bool('hello')) # ๐๏ธ True print(bool(0)) # ๐๏ธ False
The bool class converts truthy values to True
and falsy values to False
.
All values that are not truthy are considered falsy. The falsy values in Python are:
None
and False
.0
(zero) of any numeric type.""
(empty string), ()
(empty tuple), []
(empty list), {}
(empty dictionary), set()
(empty set), range(0)
(empty
range).If you need to print a boolean value or a variable storing a boolean value in a string, use a formatted string literal.
my_bool = True # โ print boolean value in a string print(f'is subscribed: {my_bool}') # ๐๏ธ is subscribed: True
f
.Make sure to wrap expressions in curly braces - {expression}
.
Alternatively, you can pass multiple, comma-separated arguments to the print()
function.
# ๐๏ธ is subscribed: True print('is subscribed:', True) # ๐๏ธ is subscribed:True print('is subscribed:', True, sep='')
print()
function, they get separated by a space.You can set the sep keyword argument to an empty string to remove the separator.
If you need to
convert a boolean value to its integer
representation, use the int()
class.
print(int(True)) # ๐๏ธ 1 print(int(False)) # ๐๏ธ 0
The int class returns an integer object constructed from the provided number or string argument.
Conversely, if you need to convert an integer to a boolean value, use the
bool()
class.
print(bool(1)) # ๐๏ธ True print(bool(0)) # ๐๏ธ False
The bool() class takes
a value and converts it to a boolean (True or False). If the provided value is
falsy or omitted, then bool
returns False
, otherwise it returns True
.
You can learn more about the related topics by checking out the following tutorials: