首页 > 解决方案 > print(variable1.format(variable2)) 代码的逻辑是如何工作的?

问题描述

例子:

hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"

print(joke_evaluation.format(hilarious))

“.format”有什么作用?为什么我们在这里使用空的 {}(大括号)?当我运行这段代码时,它是如何打印 False 的?这段代码是如何工作的?

标签: python-3.x

解决方案


格式化字符串时,您可以这样做

"sometext {}".format("whatever is supposed to be places at curly brackets place")

这使

"sometext whatever is supposed to be placed at curly brackets place"

什么是 False,你对 False 感到很有趣,python 会在形成时自动将其解析为字符串


推荐阅读