简体   繁体   English

如何在字符串格式中对十六进制执行 alignment

[英]How to do alignment on hex in string formatting

Is there a way to align hexadecimal digits in string formatting?有没有办法在字符串格式中对齐十六进制数字? I feel like it has to be quite easy, and I'm just missing the formatting.我觉得它必须很容易,我只是缺少格式。 For example:例如:

ones_comp = 72510
print (f"2. Ones comp: {ones_comp:#0x>12}")

I'd like it to print something like:我希望它打印如下内容:

2. Ones comp:       0x11b3e

You can do:你可以做:

print(f"2. Ones comp: {ones_comp:>12x}")

which outputs:输出:

2. Ones comp:        11b3e

or use the hex function if you really want the 0x prefix:如果你真的想要0x前缀,或者使用hex function :

print(f"2. Ones comp: {hex(ones_comp):>12}")

which outputs:输出:

2. Ones comp:      0x11b3e

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

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