How to convert decimal to hex in the following format?
format() function with a
>>> format(255, '02x') 'ff' >>> format(2, '02x') '02'
02 part tells
format() to use at least 2 digits and to use zeros to pad it to length,
x means lower-case hexadecimal.
The Format Specification Mini Language also gives you
X for uppercase hex output, and you can prefix the field width with
# to include a
0X prefix (depending on wether you used
X as the formatter). Just take into account that you need to adjust the field width to allow for those extra 2 characters:
>>> format(255, '02X') 'FF' >>> format(255, '#04x') '0xff' >>> format(255, '#04X') '0XFF'