How do I concatenate a boolean to a string in Python?

boolean, casting, concatenation, python, string

Solution

The recommended way is to let `str.format` handle the casting (docs). Methods with `%s` substitution may be deprecated eventually (see PEP3101).

>>> answer = True
>>> myvar = "the answer is {}".format(answer)
>>> print(myvar)
the answer is True

In Python 3.6+ you may use literal string interpolation:

 >>> print(f"the answer is {answer}")
the answer is True

Problem

I want to accomplish the following ``` answer = True myvar = "the answer is " + answer ``` and have myvar's value be "the answer is True". I'm pretty sure you can do this in Java.

Original source

Related problems