I wrote this simple function:
def padded_hex(i, l): given_int = i given_len = l hex_result = hex(given_int)[2:] # remove \'0x\' from beginning o
If just for leading zeros, you can try zfill function.
zfill
'0x' + hex(42)[2:].zfill(4) #'0x002a'