python - 打印十六进制值时如何让 Python 使用大写字母?

在 Python v2.6 中,我可以通过以下两种方式之一获得整数的十六进制:

print(("0x%x")%value)
print(hex(value))

但是,在这两种情况下,十六进制数字都是小写的。我怎样才能得到这些大写?

最佳答案

大写 X(Python 2 和 3 使用 sprintf-style formatting):

print("0x%X" % value)

或者在 python 3+ 中(使用 .format string syntax ):

print("0x{:X}".format(value))

或者在 python 3.6+ 中(使用 formatted string literals ):

print(f"0x{value:X}")

https://stackoverflow.com/questions/13277440/

相关文章:

r - 用适当的精度数字校正R中的“摘要”

r - 在 R 绘图图例中添加 pch 符号

java - 打印出数组中的元素,元素之间使用逗号

r - 自动格式化 R 代码的工具

linux - 如何在 unix/linux 中显示 'ls -l' 命令的列标题?

haskell - 如何让haskell输出不是科学计数法的数字?

objective-c - 将 [NSDate date] 的日期缩短几个小时

python - 将文本列表格式化为列

ruby-on-rails - 在 Ruby 中将换行符解释为 markdown(Github Ma

c# - 在单个键盘快捷键中使用 R# 重新格式化代码