Skip to main content
 首页 » 编程设计

Python 2 中的 Python 3 f-string 替代品

2025年05月04日10jackei

我主要使用 Python 3。我可以这样写:

print(f"The answer is {21 + 21}!") 

输出:

The answer is 42!

但是在 Python 2 中,f-strings不存在。那么下面是最好的方法吗?

print("the answer is " + str(21 + 21) + "!") 

请您参考如下方法:

使用格式:

print("答案是 {} !".format(21 + 21))