Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 682
travis1097 Avatar asked May 09 '12 04:05

travis1097


People also ask

How do you cast a Boolean to a string?

To convert Boolean to String in Java, use the toString() method. For this, firstly, we have declared two booleans. String str1 = new Boolean(bool1). toString(); String str2 = new Boolean(bool2).

How do you add a Boolean value in Python?

In Python, True == 1 and False == 0 , as True and False are type bool , which is a subtype of int . When you use the operator + , it is implicitly adding the integer values of True and False .

Does += work with strings Python?

+= operatorYou can append another string to a string with the in-place operator, += . The string on the right is concatenated after the string variable on the left. If you want to add a string to the end of a string variable, use the += operator.

How do you print a Boolean value from a string in Python?

Use the built-in bool() method to print the value of a variable.

How to convert one Boolean to string in Python?

Another way to convert one boolean to string in python : bool_true = True bool_false = False print('bool_true = '+str(bool_true)) print('bool_false = '+str(bool_false)) It will print the same output. All methods give the same output. You can use any one of them. python.

How to concatenate strings in Python?

Since Python string is immutable, the concatenation always results in a new string. To concatenate two or more literal strings, you just need to place them next to each other. For example: Note that this way won’t work for the string variables. A straightforward way to concatenate multiple strings into one is to use the + operator:

How to concatenate strings with variables in JavaScript?

In the case of strings, the + operator acts as the concatenation operator. This method also works with the strings assigned to variables. Let’s see how this looks:

How to concatenate multiple strings into one in PHP?

A straightforward way to concatenate multiple strings into one is to use the + operator: s = 'String' + ' Concatenation' print (s) Code language: PHP (php) And the + operator works for both literal strings and string variables.


2 Answers

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).

like image 112
Andrew Gorcester Avatar answered Sep 21 '22 20:09

Andrew Gorcester


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 
like image 36
wim Avatar answered Sep 20 '22 20:09

wim