Print boolean values in a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 28, 2022

banner

Photo from Unsplash

Print boolean values in a String in Python #

Use a formatted string literal to print boolean values in a string, e.g. print(f'is subscribed: {my_bool}'). Formatted string literals let us include variables inside of a string by prefixing the string with f.

main.py
my_bool = True result = f'is subscribed: {my_bool}' print(result) # 👉️ is subscribed: True my_bool_2 = False result = f'{my_bool} is the opposite of {my_bool_2}' print(result) # 👉️ True is the opposite of False

We used a formatted string literal to print variables that store boolean values in a string.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ is subscribed: True

You can use this approach to insert as many variables that store boolean values in a string as necessary.

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

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

An alternative to using a formatted string literal is to 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='')
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.

Note that you shouldn't try to use the addition (+) operator between values of type bool and str.

main.py
my_bool = True my_str = 'is subscribed: ' # ⛔️ TypeError: can only concatenate str (not "bool") to str result = my_str + my_bool
The values on the left and right-hand sides of the addition (+) operator need to be of compatible types.

To solve this, convert the boolean to a string and concatenate the two strings.

main.py
my_bool = True my_str = 'is subscribed: ' result = my_str + str(my_bool) print(result) # 👉️ is subscribed: True

We used the str() class to convert a boolean to a string, so we can concatenate the two strings and print the result.

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
my_str = 'is subscribed:' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ True my_bool = True print(type(my_bool)) # 👉️ <class 'bool'> print(isinstance(my_bool, bool)) # 👉️ 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.

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.