How do I concatenate a boolean to a string in Python? [duplicate]
Asked Answered
G

7

91

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.

Godsey answered 9/5, 2012 at 4:21 Comment(1)
fyi: bools are capitalized in Python (True, False)Polacre
D
30

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
Dictation answered 9/5, 2012 at 4:30 Comment(0)
D
141
answer = True
myvar = "the answer is " + str(answer)

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:

myvar = "the answer is %s" % answer

Note that answer must be set to True (capitalization is important).

Dodgem answered 9/5, 2012 at 4:25 Comment(0)
D
30

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
Dictation answered 9/5, 2012 at 4:30 Comment(0)
D
9
answer = True
myvar = "the answer is " + str(answer)

or

myvar = "the answer is %s" % answer
Demarcusdemaria answered 9/5, 2012 at 4:23 Comment(1)
The %s outside of quotes shouldn't be there, but this is indeed correct.Psychodiagnostics
B
3

Using the so called f strings:

answer = True
myvar = f"the answer is {answer}"

Then if I do

print(myvar)

I will get:

the answer is True

I like f strings because one does not have to worry about the order in which the variables will appear in the printed text, which helps in case one has multiple variables to be printed as strings.

Budget answered 21/3, 2018 at 18:56 Comment(0)
L
0

answer = True

myvar = 'the answer is ' + str(answer) #since answer variable is in boolean format, therefore, we have to convert boolean into string format which can be easily done using this

print(myvar)

Lierne answered 5/11, 2018 at 3:24 Comment(1)
Welcome to SO, Lijin G. Varghese! Code-only answers are discouraged here, as they provide no insight into how the problem was solved. Please update your answer with an explanation of how your code solves the problem at hand :)Inhalator
F
0

If you want JSON boolean instead of python bool, you can use json module,

print(json.dumps(True))
Frohne answered 7/12, 2023 at 16:30 Comment(0)
H
-2
answer = “True”

myvars = “the answer is” + answer

print(myvars)

That should give you the answer is True easily as you have stored answer as a string by using the quotation marks

Halona answered 26/5, 2020 at 15:54 Comment(1)
But he doesn't have the answer stored as a string. It is a boolean. The question is how to convert a dynamic boolean to its string representation.Terrorstricken

© 2022 - 2024 — McMap. All rights reserved.