Char list from 0 to f

I am writing a python script that needs a list of all hexadecimal characters.

Is it possible to do list(string.printable[:16])to get ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']?

+4
source share
3 answers

The easiest way is to list all numbers from 0 to 15, formatted as hex:

["{:x}".format(x) for x in range(0,16)]

GarbageCollector suggested a good alternative to comments that should be adapted to remove redundant upper case characters:

>>> import string
>>> string.hexdigits
'0123456789abcdefABCDEF'
>>> string.hexdigits[:16]
'0123456789abcdef'

to get the list:

>>> list(string.hexdigits[:16])

the fact that the character order remains unchanged in string.hexdigitsa future version of python is unknown. It’s still nice to know that it stringcontains several useful groups of characters.

+4
source

list('0123456789abcdef'), ?

, [f'{i:x}' for i in range(16)] .

+2

: .

Python:

string.printable

ASCII, . digits, ascii_letters, punctuation whitespace.

Python , , , .

, , , .

string.hexdigits, '0123456789abcdefABCDEF', 16 , , , .

, '01234567890abcdef' string.hexdigits[:16].

+2

Source: https://habr.com/ru/post/1692138/


All Articles