整个 ASCII 集:
In [22]: "".join(chr(x) for x in range(128))
Out[22]: '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f'
如果你想要可打印的 ascii 字符:
In [9]: "".join(chr(x) for x in range(32,127))
Out[9]: ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
或者如果你只想要字母:
In [10]: import string
In [11]: string.ascii_letters
Out[11]: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.printable
也是一个选项,它包含 5 个不在范围内的额外字符(32,127):
In [39]: s1=set(x for x in string.printable)
In [40]: s2=set(chr(x) for x in range(32,127))
In [41]: s1-s2
Out[41]: set(['\t', '\x0b', '\n', '\r', '\x0c'])