Python Glossary Русский

all Python 3stringterm

string.hexdigits

digits

The string '0123456789abcdefABCDEF'. string.octdigits is '01234567'. Used when parsing hexadecimal and octal numbers.

Syntax

string.hexdigits  # '0123456789abcdefABCDEF'
string.octdigits  # '01234567'

You do not need these constants to parse a number: int(s, 16) and int(s, 8) do the conversion themselves and raise ValueError on garbage, so the constants are better suited to pre-validation or to generating random hex. hexdigits deliberately holds both cases, so lowercasing before the check is pointless — but int() with base 16 additionally accepts a '0x' prefix and underscore separators, neither of which is in the constant, so an all-chars-in-hexdigits check will reject strings int() would happily read.

Examples

import string
print(string.hexdigits)  # → 0123456789abcdefABCDEF
print(string.octdigits)  # → 01234567
is_hex = all(c in string.hexdigits for c in 'ff00AA')  # → True
is_oct = all(c in string.octdigits for c in '0755')  # → True
print(len(string.hexdigits))  # → 22

See also

Open in the full glossarydocs.python.orgReport an inaccuracy