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

标签 python python-3.x python-2.7 f-string

我主要使用 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))

关于Python 2 中的 Python 3 f-string 替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55709210/

相关文章:

python - 不同IDE的除法结果不同

python - eBay SDK 无法识别沙盒用户帐户

python - 如何在 pyplot 中获取图形的轴列表?

python - Tensorflow 对象检测 API 裁剪图像片段

python - Pexpect PopenSpawn 在标准输出上不显示任何内容

python - 覆盖 Odoo 中的创建函数

python - 如何从 infimnist/mnist8m 数据集中读取图像和标签?

python - 使用 pygame 扭曲图像

python-3.x - 安装 scikit-learn 时卡在 "Installing build dependencies ..."

python - 我如何使用字典理解来计算文档中每个单词的出现次数