Python中的%i和%d有什么区别?
好的,我正在研究数字格式,发现您可以使用%d 或%i 格式化整数.例如:
OK I was looking into number formatting and I found that you could use %d or %i to format an integer. For example:
number = 8
print "your number is %i." % number
或
number = 8
print "your number is %d." % number
但是有什么区别?我的意思是我发现了一些东西,但那完全是胡言乱语.有人在这里说轻密码或英语吗?
But what is the difference? I mean i found something but it was total jibberish. Anyone speak Mild-Code or English here?
Python复制了C格式说明.
Python copied the C formatting instructions.
对于输出,在Python和C中,%i
和%d
是完全相同的东西.
For output, %i
and %d
are the exact same thing, both in Python and in C.
区别在于,当您使用它们通过scanf()
函数在C中使用它们来解析 input 时,它们会做什么.请参见 printf中格式符%i和%d之间的差异.
The difference lies in what these do when you use them to parse input, in C by using the scanf()
function. See Difference between format specifiers %i and %d in printf.
Python没有等效的scanf
,但是Python字符串格式化操作保留了两个选项以保持与C的兼容性.
Python doesn't have a scanf
equivalent, but the Python string formatting operations retained the two options to remain compatible with C.
新的str.format()
和format()
格式规范迷你语言放弃了对i
的支持,并且只保留了d
.
The new str.format()
and format()
format specification mini-language dropped support for i
and stuck with d
only.