使用新的
.format()字符串方法:
>>> "{0:#0{1}x}".format(42,6)'0x002a'
说明:
{ # Format identifier0: # first parameter# # use "0x" prefix0 # fill with zeroes{1} # to a length of n characters (including 0x), defined by the second parameterx # hexadecimal number, using lowercase letters for a-f} # End of format identifier
如果您希望字母十六进制数字为大写字母,但前缀为小写字母“ x”,则需要一些解决方法:
>>> '0x{0:0{1}X}'.format(42,4)'0x002A'
从Python 3.6开始,您还可以执行以下 *** 作:
>>> value = 42>>> padding = 6>>> f"{value:#0{padding}x}"'0x002a'
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)