问题
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
, orL
) 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