89DEVs

Python: How to convert boolean to string?

How to convert boolean to string in Python? Use the str() function.


myBoolean = True
myStr = str(myBoolean)
print("the answer is " + myStr)


the answer is True

If the str() function is not used a TypeError exception is raised, because Python can only concatenate string to string.

TypeError: can only concatenate str (not "bool") to str

Use the type() function to validate the data type.


myBoolean = True
myStr = str(myBoolean)
print(type(myStr))