Python Int to Hex | String Formatting

πŸ’¬ Question: How to use Python’s string formatting capabilities — f-strings, percentage operator, format(), string.format() — to convert an integer to a hexadecimal string?

Lowercase Solution without ‘0x’ Prefix

You can convert an integer my_int to a simple lowercase hex string without '0x' prefix by using any of the four string formatting variants—all based on the hexadecimal string formatting symbol x:

  1. f'{my_int:x}'
  2. '%x'%my_int
  3. '{:x}'.format(my_int)
  4. format(my_int, 'x')

Here are those four ways exemplified converting the integer 255 to the hex string 'ff':

>>> my_int = 255
>>> f'{my_int:x}'
'ff'
>>> '%x'%my_int
'ff'
>>> '{:x}'.format(my_int)
'ff'
>>> format(my_int, 'x')
'ff'

Lowercase Solution with ‘0x’ Prefix

You can convert an integer my_int to a simple lowercase hex string with '0x' prefix by using any of the four string formatting variants—all based on the hexadecimal string formatting symbol x:

  1. f'0x{my_int:x}'
  2. '0x%x'%my_int
  3. '0x{:x}'.format(my_int)
  4. '0x' + format(my_int, 'x')

Here are those four ways exemplified converting the integer 255 to the hex string '0xff':

>>> my_int = 255
>>> f'0x{my_int:x}'
'0xff'
>>> '0x%x'%my_int
'0xff'
>>> '0x{:x}'.format(my_int)
'0xff'
>>> '0x' + format(my_int, 'x')
'0xff'

Uppercase Solution without ‘0x’ Prefix

You can convert an integer my_int to a simple uppercase hex string without '0x' prefix by using any of the four string formatting variants—all based on the uppercase heXadecimal string formatting symbol X:

  1. f'{my_int:X}'
  2. '%X'%my_int
  3. '{:X}'.format(my_int)
  4. format(my_int, 'X')

Here are those four ways exemplified converting the integer 255 to the hex string 'FF':

>>> my_int = 255
>>> f'{my_int:X}'
'FF'
>>> '%X'%my_int
'FF'
>>> '{:X}'.format(my_int)
'FF'
>>> format(my_int, 'X')
'FF'

Uppercase Solution with ‘0x’ Prefix

You can convert an integer my_int to a simple uppercase hex string with '0x' prefix by using any of the four string formatting variants—all based on the uppercase heXadecimal string formatting symbol X:

  1. f'0x{my_int:X}'
  2. '0x%X'%my_int
  3. '0x{:X}'.format(my_int)
  4. '0x' + format(my_int, 'X')

Here are those four ways exemplified converting the integer 255 to the hex string '0xFF':

>>> my_int = 255
>>> f'0x{my_int:X}'
'0xFF'
>>> '0x%X'%my_int
'0xFF'
>>> '0x{:X}'.format(my_int)
'0xFF'
>>> '0x' + format(my_int, 'X')
'0xFF'

Fixed-Width Hex String with ‘0’ Padding

You can use the format specifer 02X to convert an integer to a fixed-width hex string with 2 positions, and filling the remaining positions with '0' symbols from the left. For example, the format specifier 03x creates a hex string of width 3.

Here’s how that works for f-strings:

>>> f'{my_int:02X}'
'FF'
>>> f'{my_int:01X}'
'FF'
>>> f'{my_int:05X}'
'000FF'

Analogously, the same format specifier can be applied to all other string formatting capabilities such as format(), string.format(), and percentage operator:

>>> '0x' + format(my_int, '04X')
'0x00FF'
>>> '0x{:04X}'.format(my_int)
'0x00FF'
>>> '0x%04X'%my_int
'0x00FF'
>>> f'0x{my_int:04X}'
'0x00FF'

This creates an uppercase, fixed-width hex string with 4 positions and padded with 0s from the left.

🌍 Recommended Tutorial: Python Integer to Hex String (Easy)