How to print Boolean values in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Print boolean values in Python

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).

main.py
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

print boolean values in python

The code for this article is available on GitHub

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.

main.py
my_bool = True print(my_bool) # ๐Ÿ‘‰๏ธ True print(False) # ๐Ÿ‘‰๏ธ False
Note that the print() function returns None, so don't try to store the result of calling print in a variable.
main.py
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.

main.py
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.

main.py
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

checking what type a variable stores

The code for this article is available on GitHub

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.

# Converting a value to a boolean and printing the result

If you need to convert a value to a boolean and print the result, use the bool() class.

main.py
print(bool('hello')) # ๐Ÿ‘‰๏ธ True print(bool(0)) # ๐Ÿ‘‰๏ธ False

converting a value to a boolean and printing the result

The code for this article is available on GitHub

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:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type.
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

# Printing a boolean value in a String

If you need to print a boolean value or a variable storing a boolean value in a string, use a formatted string literal.

main.py
my_bool = True # โœ… print boolean value in a string print(f'is subscribed: {my_bool}') # ๐Ÿ‘‰๏ธ is subscribed: True

printing boolean value in string

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

# Passing multiple, comma-separated arguments to print()

Alternatively, you can pass multiple, comma-separated arguments to the print() function.

main.py
# ๐Ÿ‘‡๏ธ is subscribed: True print('is subscribed:', True) # ๐Ÿ‘‡๏ธ is subscribed:True print('is subscribed:', True, sep='')

passing multiple comma separated arguments to print

The code for this article is available on GitHub
By default, when you pass multiple, comma-separated arguments to the 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.

main.py
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.

main.py
print(bool(1)) # ๐Ÿ‘‰๏ธ True print(bool(0)) # ๐Ÿ‘‰๏ธ False
The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev