I write a python script, which needs a list of all hexadecimal characters.
Is it a good idea to do list(string.printable[:16])
to get ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
?
The simplest way would be a list comprehension of all numbers from 0 to 15 formatted as hex:
["{:x}".format(x) for x in range(0,16)]
User GarbageCollector suggested a nice alternative in comments, which must be adapted to remove the redundant, uppercased chars:
>>> import string
>>> string.hexdigits
'0123456789abcdefABCDEF'
>>> string.hexdigits[:16]
'0123456789abcdef'
to get a list:
>>> list(string.hexdigits[:16])
the fact that the order of the characters remains the same in string.hexdigits
in future python version is however unknown. Still nice to know that string
has some many useful characters groups.
How about doing list('0123456789abcdef')
to make it explicit?
If you don't want to spell it out, [f'{i:x}' for i in range(16)]
should also work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With