Search code examples
pythonstring-formatting

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


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?


Solution

  • 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.