2012-05-09 8 views

risposta

77
answer = True 
myvar = "the answer is " + str(answer) 

Python non fa colata implicito, come implicito colata può mascherare errori logici critici. Basta lanciare risposta ad una stringa stessa per ottenere la sua rappresentazione di stringa ("true"), oppure utilizzare la formattazione delle stringhe in questo modo:

myvar = "the answer is %s" % answer 

notare che la risposta deve essere impostato su True (capitalizzazione è importante).

7
answer = True 
myvar = "the answer is " + str(answer) 

o

myvar = "the answer is %s" % answer 
+0

Il '% s' al di fuori di virgolette non dovrebbe essere lì, ma questo è davvero corretto. – Makoto

+0

Oops, corretto l'errore di battitura – Squazic

9

Il modo consigliato è di consentire a str.format di gestire il casting (docs). I metodi con la sostituzione %s possono essere alla fine deprecati (vedere PEP3101).

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