all Python 3stringterm
string.digits
digits
The string '0123456789'. Used to test for digit characters and to generate numeric codes.
Syntax
string.digits # '0123456789'
Watch out with in: this is a plain string, so '12' in string.digits is True (it is a substring), while '21' is False. To test a whole string use s.isdigit() or set(s) <= set(string.digits). Note also that the constant is ASCII-only: an Arabic-Indic digit like '٣' passes isdigit() but is absent from string.digits, which makes a membership test against it stricter than isdigit().
Examples
import string
print(string.digits) # → 0123456789
print('5' in string.digits) # → True
digits_only = [c for c in 'abc123' if c in string.digits] # → ['1','2','3']
import string
import random
pin = ''.join(random.choices(string.digits, k=4)) # → '4-значный PIN'
print(len(string.digits)) # → 10
See also
Open in the full glossarydocs.python.orgReport an inaccuracy