'h' 在 python 格式字符串中是什么意思? [英] What does 'h' mean in a python format string?

查看:84
本文介绍了'h' 在 python 格式字符串中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>>>>wierd_format = '[%27he]'>>>打印 wierd_format % 2.5[ 2.500000e+00]

但这不是:

<预><代码>>>>bad_format = '[%20qe]'>>>打印 bad_format % 2.5回溯(最近一次调用最后一次):文件prog.py",第 5 行,在 <module> 中打印 bad_format % 2.5ValueError: 不支持的格式字符 'q' (0x71) 在索引 4

显然,h 是支持的格式字符.但是,文档 没有提到 h 说明符.它有什么作用?

解决方案

来自文档:

<块引用>

长度修饰符(hlL)可能存在,但被忽略,因为它对于 Python 不是必需的 –所以例如%ld%d 相同.

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?

解决方案

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.

这篇关于'h' 在 python 格式字符串中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆