且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Python中将布尔值连接到字符串?

更新时间:2023-11-07 18:52:22

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