Skip to content

Python print string with Boolean | Example code

  • by

Use string casting to print a string with Boolean in Python.

str(BooleanValue)

Problem and Solution for print string with Boolean in Python

Simple python example code. Boolean formatted in Strings.

Problem

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

answer = True
print("The answer is " + answer)

Solution

Python does not do implicit casting, as implicit casting can mask critical logic errors. Just cast answer to a string itself to get its string representation (“True”), or use string formatting like so:

answer = True
print("The answer is " + str(answer))

Output:

Python print string with Boolean

Do comment if you have another example and doubts on this Python String Boolean code.

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