Python 3.6+确实具有变量插值-f
在您的字符串前添加:
f"foo is {bar}"
对于低于此版本的Python版本(Python 2-3.5),您可以str.format
用来传递变量:
# Rather than this:
print("foo is #{bar}")
# You would do this:
print("foo is {}".format(bar))
# Or this:
print("foo is {bar}".format(bar=bar))
# Or this:
print("foo is %s" % (bar, ))
# Or even this:
print("foo is %(bar)s" % {"bar": bar})