Skip to content

Python print Boolean | Example code

  • by

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.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading