[Python-ideas] Add an option for delimiters in bytes.hex()

Nick Coghlan ncoghlan at gmail.com
Mon May 1 09:38:20 EDT 2017

On 1 May 2017 at 17:19,  <robert.hoelzl at posteo.de> wrote:
> The bytes.hex() function is the inverse function of Bytes.fromhex().
> But fromhex can process spaces (which is much more readable), while hex()
> provides no way to include spaces.
> My proposal would be to add an optional delimiter, that allows to specify a
> string that will be inserted between the digit pairs of a byte:
> def hex(self, delimiter=‘‘): …

We're definitely open to offering better formatting options for bytes.hex().

My proposal in https://bugs.python.org/issue22385 was to define a new
formatting mini-language (akin to the way strftime works, but with a
much simpler formatting mini-language):

However, a much simpler alternative would be to just support two
keyword arguments to hex(): "delimiter" (as you suggest) and
"chunk_size" (defaulting to 1, so you get per-byte chunking by


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list