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

travis1097 :

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.

Andrew Gorcester :
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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

How do I concatenate an escape to a string?

How do I concatenate a string with a variable?

How do I concatenate a string in handlebars view

How do I concatenate string and an integer?

How can I concatenate a string and a number in Python?

How do I concatenate files in Python?

How do I concatenate two lists in Python?

Swift: how do I concatenate a [[String]] to a [String] the right way?

In Rust, how do I concatenate one String with a linefeed and then another String?

How do I use a Boolean in Python?

How to concatenate a string in python?

How to concatenate string in python

How do I do a boolean string comparison in SQL Server 2008

how do I concatenate sub strings to make a bigger string

How do I concatenate a string with a value returned from a mule expression?

How do I concatenate a string stored in variable and a number in MATLAB

How do I concatenate a date cell (independant of locale) to a string in Excel?

how do I concatenate string with list in list items?

How do I concatenate two string macros in C?

How do I concatenate string and convert to upper case

How do I concatenate a string to a variable declaration in Ruby?

PHP: How do I concatenate a foreach inside a string?

How do i concatenate a string in between to strings at run time?

Python: How can I concatenate in a bytearray as I do with strings?

How do I evaluate if a string is a number or boolean in Haxe?

How do I concatenate two lists inside nested list Python?

How do I concatenate strings of rounded numbers python?

How do I correctly concatenate 3 dataframes extracted with a loop in python?