Use the format()
function:
format(addrint, '#04x')
This formats the input value as a 2 digit hexadecimal string with leading zeros to make up the length, and #
includes the 'standard prefix', 0x
in this case. Note that the width of 4 includes that prefix. x
produces lower-case hex strings; use X
if you need uppercase.
Demo:
>>> for i in range(8, 12):
... print format(i, '#04x')
...
0x08
0x09
0x0a
0x0b