From the docs:
The '#' option causes the “alternate form” to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float and complex types. For integers, when binary, octal, or hexadecimal output is used, this option adds the respective prefix '0b', '0o', '0x', or '0X' to the output value.
And then later:
width is a decimal integer defining the minimum total field width, including any prefixes, separators, and other formatting characters. If not specified, then the field width will be determined by the content.
Note "including any prefixes"
.
And then finally:
When no explicit alignment is given, preceding the width field by a zero ('0') character enables sign-aware zero-padding for numeric types.
And for the x
type:
Hex format. Outputs the number in base 16, using lower-case letters for the digits above 9.
Putting this all together, we can use:
TVAR = 0x000a
print(f"{TVAR:#06x}")
which outputs:
0x000a
Admittedly, I personally find the docs on the Format Spec mini-language hard to parse in certain sections as it involves a lot of jumping around to find a specific case you're interested in.