标准库string中,capwords是函数,Formatter, Template是类,其余9个是字符串常量:
>>> from string import * >>> string.__all__ ['ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace', 'Formatter', 'Template'] >>> ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> ascii_lowercase 'abcdefghijklmnopqrstuvwxyz' >>> ascii_uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> capwords <function capwords at 0x022FA730> >>> digits '0123456789' >>> hexdigits '0123456789abcdefABCDEF' >>> octdigits '01234567' >>> printable '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c' >>> punctuation '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' >>> whitespace ' \t\n\r\x0b\x0c' >>> Formatter <class 'string.Formatter'> >>> Template <class 'string.Template'> >>> Formatter()
分别是:字母、小写字母、大写字母、数字、十六进制数字、八进制数字、可打印字符、标点、空格
用法:
>>> a='a' >>> b='3' >>> c='\\' >>> a in ascii_letters True >>> b in ascii_letters False >>> c in digits False >>> c in punctuation True >>> b in digits True >>> '\x0b' in whitespace True >>> ' ' in whitespace True >>> '' in whitespace True >>> None is whitespace False >>> '' in digits True >>> '' in ascii_letters True >>> '' in punctuation True >>>
注意:''空串都返回True ......
其实不用string库,自动动手创建这些常量也非常简单:
>>> digits=''.join([chr(i) for i in range(48,58)]) >>> digits '0123456789' >>> ascii_lowercase=''.join([chr(i) for i in range(97,123)]) >>> ascii_lowercase 'abcdefghijklmnopqrstuvwxyz' >>> ascii_uppercase=''.join([chr(i) for i in range(65,91)]) >>> ascii_uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> ascii_letters=''.join([chr(i) for i in range(65,123) if not 91<=i<97]) >>> ascii_letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' >>> #or: >>> tmp=''.join([chr(i) for i in range(122,64,-1) if not 91<=i<97]) >>> tmp[:26][::-1]+tmp[26:][::-1] 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> punctuation=''.join([chr(i) for i in range(33,127) if any([i>122, 90<i<97, 57<i<65, i<48])]) >>> punctuation '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' >>>
还有''.join([chr(i) for i in range(33,127)]) + 6个空白符就是printable,共100个可打印字符。