Padding a hexadecimal string with zeros
Perhaps you're looking for the .zfill method on strings. From the docs:


Help on built-in function zfill:

zfill(...)
    S.zfill(width) -&gt; string

    Pad a numeric string S with zeros on the left, to fill a field
    of the specified width.  The string S is never truncated.



Your code can be written as:

def padhexa(s):
    return '0x' + s[2:].zfill(8)

assert '0x00000123' == padhexa('0x123')
assert '0x00ABCD12' == padhexa('0xABCD12')
assert '0x12345678' == padhexa('0x12345678')