【发布时间】:2015-09-03 13:01:44
【问题描述】:
我只是在学习 python 并尝试一个非常简单的字符串格式化行,但它不能正常工作。从下面的输出中可以看出,它在 3 个中的最后一个插入数字,但前 2 个仅显示代码而不是数字。我确信解决方案很简单,但我尝试查看我的代码与我拥有的学习材料中的代码,我看不出有任何区别。在 Visual Studio 2015 中使用 Python 3.4。
提前感谢您提供任何可用的帮助! :)
代码(面积为 200)
print("The area of the square would be {0:f} " .format(area))
#This is an example with multiple numbers and use of "\" outside of a string to allow for
#multiple lines of code in the same line spread out over numerous lines
print("Here are three other numbers." + \
" First number is {0:d}, second number is {1:d}, \n" + \
"third number is {2:d}" .format(7,8,9))
输出
正方形的面积是 200.000000
这里还有另外三个数字。第一个数字是 {0:d},第二个数字是 {1:d}, 第三个数字是 9。
线程“MainThread”(0xc10) 已退出,代码为 0 (0x0)。
程序“[4968] python.exe”已退出,代码为 -1073741510 (0xc000013a)。
【问题讨论】:
标签: python python-3.x string-formatting