如何在Python中将布尔值连接到字符串? [英] How do I concatenate a boolean to a string in Python?
本文介绍了如何在Python中将布尔值连接到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想完成以下任务
answer = True
myvar = "the answer is " + answer
,并将myvar的值设为答案为True".我很确定您可以使用Java做到这一点.
and have myvar's value be "the answer is True". I'm pretty sure you can do this in Java.
推荐答案
answer = True
myvar = "the answer is " + str(answer)
Python不会进行隐式转换,因为隐式转换可以掩盖严重的逻辑错误.只需将答案强制转换为字符串本身即可获得其字符串表示形式("True"),或使用如下所示的字符串格式:
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
请注意,答案必须设置为True
(大写字母很重要).
Note that answer must be set to True
(capitalization is important).
这篇关于如何在Python中将布尔值连接到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文