Search code examples
pythonstringstring-interpolation

what does '%0*x' % (int1, int2) syntax do in python?


I know it is string interpolation, but I can't understand what is the exact meaning. It seems it convert int2 to hexadecimal as a string(I see this behavior in debugging) but what is the int1 role? (I guess it is the size of hexadecimal) can anybody explain in detail?


Solution

  • * means to get the field width from the corresponding argument, rather than hard-coding it into the format string (e.g. %08x means to print 8 hex digits). So int1 specifies the number of digits to print.

    The 0 before the field width means to pad the output with zeroes rather than spaces.