与其像这样列出字母表字符列表,不如:
alpha = ['a', 'b', 'c', 'd'.........'z']
我们有没有办法把它归为一个范围或什么的?例如,对于数字,可以使用range()
进行分组:
range(1, 10)
与其像这样列出字母表字符列表,不如:
alpha = ['a', 'b', 'c', 'd'.........'z']
我们有没有办法把它归为一个范围或什么的?例如,对于数字,可以使用range()
进行分组:
range(1, 10)
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
如果你真的需要一份 list :
>>> list(string.ascii_lowercase)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
用range
美元
>>> list(map(chr, range(97, 123))) #or list(map(chr, range(ord('a'), ord('z')+1)))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
其他有用的string
模块功能:
>>> help(string) # on Python 3
....
DATA
ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'
hexdigits = '0123456789abcdefABCDEF'
octdigits = '01234567'
printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
whitespace = ' \t\n\r\x0b\x0c'