我想要完成以下工作
answer = True
myvar = "the answer is " + answer并将myvar的值设置为“答案为真”。我非常确定你可以在Java中做到这一点。
发布于 2012-05-09 12:30:59
推荐的方式是让str.format处理类型转换(docs)。具有%s替换的方法最终可能会被弃用(请参阅PEP3101)。
>>> answer = True
>>> myvar = "the answer is {}".format(answer)
>>> print(myvar)
the answer is True在Python 3.6+中,您可以使用literal string interpolation
>>> print(f"the answer is {answer}")
the answer is True发布于 2012-05-09 12:25:09
answer = True
myvar = "the answer is " + str(answer)Python不进行隐式转换,因为隐式转换可以掩盖关键的逻辑错误。只需将答案强制转换为字符串本身即可获得其字符串表示形式("True"),或者使用如下格式的字符串:
myvar = "the answer is %s" % answer请注意,答案必须设置为True (大小写很重要)。
发布于 2012-05-09 12:23:44
answer = True
myvar = "the answer is " + str(answer)或
myvar = "the answer is %s" % answerhttps://stackoverflow.com/questions/10509803
复制相似问题