Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么是';h';是指python格式的字符串吗?_Python_String Formatting - Fatal编程技术网

什么是';h';是指python格式的字符串吗?

什么是';h';是指python格式的字符串吗?,python,string-formatting,Python,String Formatting,这是一个有效的python格式字符串: >>> wierd_format = '[%27he]' >>> print wierd_format % 2.5 [ 2.500000e+00] 但这不是: >>> bad_format = '[%20qe]' >>> print bad_format % 2.5 Traceback (most recent call last): File "prog.py",

这是一个有效的python格式字符串:

>>> wierd_format = '[%27he]'
>>> print wierd_format % 2.5
[        2.500000e+00]
但这不是:

>>> 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
>>错误的_格式='[%20qe]'
>>>打印错误的\u格式%2.5
回溯(最近一次呼叫最后一次):
文件“prog.py”,第5行,在
打印错误的\u格式%2.5
ValueError:索引4处的格式字符“q”(0x71)不受支持
显然,
h
是受支持的格式字符。但是,没有提到
h
说明符。它做什么?

来自文档:

长度修饰符(
h
l
l
)可能存在,但会被忽略,因为它对于Python来说不是必需的–例如,
%ld
%d
相同

假设它是一个长度修改器

长度修饰符(h、l或l)可能存在,但会被忽略,因为Python不需要它。因此,例如%ld与%d相同

他们看起来一样

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

哎呀,我瞎了。那个文档链接比我的更过时,但两者都包含string@Eric:谢谢。已将其更新为较新的版本。:)@Eric:我还可以在TcL中找到
h
修饰符。这里可能有关系,但不确定。这是我的建议。Quoted->
如果是h,则指定在转换之前应将数值截断为16位值。
显然是盲的。感谢没有问题,发生了:)