例如,我了解您可以做到:
x = input("Enter an integer: ")
print("I am now printing variable x: {}".format(x))
要获取我放在其中的任何字符串的输出,因为{}只是变量的占位符。
但是,在一个网站上,我看到{:d}被使用,尽管在Python中进行了试验以弄清楚它的作用,但我似乎找不到它的用途。为什么在打印变量时使用{:d}?它有什么用?
答案 0 :(得分:0)
在python中,您可以在尖括号内指定格式。
您可以做
之类的事情>>> '{:b}'.format(2)
'10'
在您的情况下,d打印为十进制整数。
您可以在此处找到所有文档
https://docs.python.org/2/library/string.html#format-specification-mini-language