What does 'h' mean in a python format string?

大城市里の小女人 提交于 2019-12-10 14:58:33

问题


This is a valid python format string:

>>> wierd_format = '[%27he]'
>>> print wierd_format % 2.5
[        2.500000e+00]

But this isn't:

>>> bad_format = '[%20qe]'
>>> print bad_format % 2.5
Traceback (most recent call last):
  File "prog.py", line 5, in <module>
    print bad_format % 2.5
ValueError: unsupported format character 'q' (0x71) at index 4

Clearly, h is a supported format character. However, the documentation doesn't mention an h specifier. What does it do?


回答1:


From the docs:

A length modifier (h, l, or L) may be present, but is ignored as it is not necessary for Python – so e.g. %ld is identical to %d.




回答2:


Python Docs say that it is a length modifier.

A length modifier (h, l, or L) may be present, but is ignored as it is not necessary for Python. so e.g. %ld is identical to %d.

They seem the same,

>>> "[%he]" %2.5
'[2.500000e+00]'
>>> "[%le]" %2.5
'[2.500000e+00]'
>>> "[%Le]" %2.5
'[2.500000e+00]'


来源:https://stackoverflow.com/questions/18174827/what-does-h-mean-in-a-python-format-string

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