简体   繁体   English

在python格式字符串中'h'是什么意思?

[英]What does 'h' mean in a python format string?

This is a valid python format string: 这是一个有效的python格式字符串:

>>> 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. 显然, h是受支持的格式字符。 However, the documentation doesn't mention an h specifier. 但是, 文档没有提到h说明符。 What does it do? 它有什么作用?

From the docs: 来自文档:

A length modifier ( h , l , or L ) may be present, but is ignored as it is not necessary for Python – so eg %ld is identical to %d . 可以存在长度修饰符( hlL ),但忽略它,因为它不是Python所必需的 - 因此例如%ld%d相同。

Python Docs say that it is a length modifier. Python Docs说它是一个长度修饰符。

A length modifier (h, l, or L) may be present, but is ignored as it is not necessary for Python. 可以存在长度修饰符(h,l或L),但忽略它,因为它不是Python所必需的。 so eg %ld is identical to %d. 所以例如%ld与%d相同。

They seem the same, 他们看起来一样,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM