0

Let's say:

hex(89)

will return

0x59

Is it possible somehow to make it always return a 4 character string? I need it as 0x0059, not 0x59. Also, if the result is 0x67F for example, I need it as 0x067F and if it already is 4 characters after 0x then no zeros need to be added. How to do that?

musava_ribica
  • 476
  • 1
  • 5
  • 18
  • Does this answer your question? [Decorating Hex function to pad zeros](https://stackoverflow.com/questions/12638408/decorating-hex-function-to-pad-zeros) – user202729 May 01 '20 at 14:29
  • Please check the options already given [here](https://stackoverflow.com/questions/12638408/decorating-hex-function-to-pad-zeros) – starturtle May 01 '20 at 14:31

1 Answers1

0

For a simple case like yours might be enough to do:

decimal_num = 89
f"0x{decimal_num:04x}"
"0x{:04x}".format(decimal_num)

Apart from the official string formatting info, there is some examples in https://pyformat.info/

hectorcanto
  • 1,987
  • 15
  • 18