Python 中 %i 和 %d 有什么区别? [复制]

2023-12-27

好的,我正在研究数字格式,我发现你可以使用%d or %i格式化一个整数。例如:

number = 8
print "your number is %i." % number

or

number = 8
print "your number is %d." % number

但有什么区别呢?我的意思是我发现了一些东西,但那完全是胡言乱语。这里有人会说轻度代码或英语吗?


Python 复制了 C 格式化指令。

For output, %i and %d在 Python 和 C 中都是完全相同的事情。

区别在于当您使用它们来解析时它们会做什么input,在 C 中使用scanf()功能。看printf 中格式说明符 %i 和 %d 之间的区别 https://stackoverflow.com/questions/1893490/difference-between-format-specifiers-i-and-d-in-printf.

Python 没有scanf等效,但 Python 字符串格式化操作保留了两个选项以保持与 C 兼容。

The new str.format() and format() 格式规范迷你语言 http://docs.python.org/2/library/string.html#formatspec放弃支持i并坚持d only.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 中 %i 和 %d 有什么区别? [复制] 的相关文章

随机推荐