Python print Boolean | Example code

You can simply print the Boolean value in python. See below code:

bol = True

print(bol)

Output: True

Print a boolean With String

If you try the print Boolean value with the string it will throw an error.

bol = True

print("Hello "+ bol)

Output: can only concatenate str (not “bool”) to str

Python print Boolean

Solution 1

Use string casting

bol = True

print("Hello " + str(bol))

Solution 2

Using comma.

bol = True

print("Hello ", bol)

Print Boolean values of different data types in Python

Use the built-in bool() method to print the value of a variable.

test = []
print(test, 'is', bool(test))

test = [0]
print(test, 'is', bool(test))

test = 0.0
print(test, 'is', bool(test))

test = None
print(test, 'is', bool(test))

test = True
print(test, 'is', bool(test))

test = 'Easy string'
print(test, 'is', bool(test))

Output:

Print Boolean values of different data types in Python

Print bool value based on expression outcome

print(1 > 9)
print(1 == 9)
print(1 < 9)

Output:

False
False
True

Do comment if you have any doubts and suggestions on this Python Boolean topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.