What is the difference between %i and %d in Python? [duplicate]

空扰寡人 提交于 2019-12-20 20:02:21

问题


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

or

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?


回答1:


Python copied the C formatting instructions.

For output, %i and %d are the exact same thing, both in Python and in C.

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 doesn't have a scanf equivalent, but the Python string formatting operations retained the two options to remain compatible with C.

The new str.format() and format() format specification mini-language dropped support for i and stuck with d only.




回答2:


There isn't any, see the Python String Formatting Manual: http://docs.python.org/2/library/stdtypes.html#string-formatting




回答3:


No difference.

And here's the proof.

The reason why there are two is that, %i is just an alternative to %d ,if you want to look at it at a high level (from python point of view).

Here's what python.org has to say about %i: Signed integer decimal.

And %d: Signed integer decimal.

%d stands for decimal and %i for integer.

but both are same, you can use both.



来源:https://stackoverflow.com/questions/17680256/what-is-the-difference-between-i-and-d-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!