在 Python 中,这样写是很乏味的:
print "foo is" + bar + '.'
我可以在 Python 中做这样的事情吗?
print "foo is #{bar}."
原文由 mko 发布,翻译遵循 CC BY-SA 4.0 许可协议
在 Python 中,这样写是很乏味的:
print "foo is" + bar + '.'
我可以在 Python 中做这样的事情吗?
print "foo is #{bar}."
原文由 mko 发布,翻译遵循 CC BY-SA 4.0 许可协议
2 回答5.2k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
3 回答1.2k 阅读✓ 已解决
2 回答862 阅读✓ 已解决
1 回答1.7k 阅读✓ 已解决
Python 3.6+ 确实有变量插值 - 在你的字符串前加上
f
:对于低于此版本的 Python (Python 2 - 3.5),您可以使用
str.format
传递变量: