How to Make a List of Alphabet Characters in Python
Created
Modified
Using string Module
The string.ascii_lowercase
is a pre-initialized string used as string constant. In Python, string ascii_lowercase will give the lowercase letters ‘abcdefghijklmnopqrstuvwxyz’.
See the following example:
#!/usr/bin/python3
# Import module
import string
# string.ascii_lowercase
# string.ascii_uppercase
# string.ascii_letters
ls = list(string.ascii_lowercase)
print(ls)
['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']
Using range Function
The range(start, stop[, step])
function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
For example,
#!/usr/bin/python3
ls = list(map(chr, range(97, 123)))
print(ls)
['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 module features:
Object type:
string.whitespace \t\n\r\v\fstring.ascii_lettersabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZstring.ascii_lowercaseabcdefghijklmnopqrstuvwxyzstring.ascii_uppercaseABCDEFGHIJKLMNOPQRSTUVWXYZstring.digits0123456789string.hexdigits0123456789abcdefABCDEFstring.octdigits01234567string.printabledigits + ascii_letters + punctuation + whitespacestring.punctuation!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~