I am confused at the difference between the Python string formatters %d and %i. The Python manual page explains them as the same thing, 'signed integer decimal' - confused !
我对Python字符串格式化程序%d和%i之间的区别感到困惑。 Python手册页将它们解释为同样的事情,'带符号整数十进制' - 混淆了!
From the Python 'Built in Types' page
从Python的“内置类型”页面
1 个解决方案
#1
They are the exact same thing.
它们完全相同。
Python inherited the syntax from C, where you can also use the same format in the scanf()
function to parse input into variables. There you can then use %i
to accept integers in hexadecimal format with a 0x
prefix, octal notation when preceded by 0
, as well as regular decimal (base 10) integers, while the %d
formatter strictly only accepts decimal integer input.
Python从C继承了语法,您也可以在scanf()函数中使用相同的格式来解析输入到变量中。然后,您可以使用%i接受带有0x前缀的十六进制格式的整数,带前缀为0的八进制表示法,以及常规十进制(基数10)整数,而%d格式化程序严格只接受十进制整数输入。
On output with the printf
function however, you'd use %x
and %o
to format output to hexadecimal and octal, explicitly, always, and %d
and %i
are merely synonymous and output decimal integers. Python inherited the printf
formats wholesale, aliases and all, even though there is no equivalent for the scanf
distinction.
但是在使用printf函数输出时,您将使用%x和%o将输出格式化为十六进制和八进制,显式,始终,%d和%i仅仅是同义和输出十进制整数。 Python继承了printf格式批发,别名和所有,即使没有与scanf区别的等价物。
See the printf
and scanf
manpages.
请参阅printf和scanf联机帮助页。
The newer Format Specification Mini-language (used in both the str.format()
method and the format()
function) does away with the i
formatter altogether; only d
remains there.
较新的格式规范迷你语言(在str.format()方法和format()函数中使用)完全取消了i格式化程序;只有d留在那里。
#1
They are the exact same thing.
它们完全相同。
Python inherited the syntax from C, where you can also use the same format in the scanf()
function to parse input into variables. There you can then use %i
to accept integers in hexadecimal format with a 0x
prefix, octal notation when preceded by 0
, as well as regular decimal (base 10) integers, while the %d
formatter strictly only accepts decimal integer input.
Python从C继承了语法,您也可以在scanf()函数中使用相同的格式来解析输入到变量中。然后,您可以使用%i接受带有0x前缀的十六进制格式的整数,带前缀为0的八进制表示法,以及常规十进制(基数10)整数,而%d格式化程序严格只接受十进制整数输入。
On output with the printf
function however, you'd use %x
and %o
to format output to hexadecimal and octal, explicitly, always, and %d
and %i
are merely synonymous and output decimal integers. Python inherited the printf
formats wholesale, aliases and all, even though there is no equivalent for the scanf
distinction.
但是在使用printf函数输出时,您将使用%x和%o将输出格式化为十六进制和八进制,显式,始终,%d和%i仅仅是同义和输出十进制整数。 Python继承了printf格式批发,别名和所有,即使没有与scanf区别的等价物。
See the printf
and scanf
manpages.
请参阅printf和scanf联机帮助页。
The newer Format Specification Mini-language (used in both the str.format()
method and the format()
function) does away with the i
formatter altogether; only d
remains there.
较新的格式规范迷你语言(在str.format()方法和format()函数中使用)完全取消了i格式化程序;只有d留在那里。