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\f
string.ascii_letters
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
string.ascii_lowercase
abcdefghijklmnopqrstuvwxyz
string.ascii_uppercase
ABCDEFGHIJKLMNOPQRSTUVWXYZ
string.digits
0123456789
string.hexdigits
0123456789abcdefABCDEF
string.octdigits
01234567
string.printable
digits + ascii_letters + punctuation + whitespace
string.punctuation
!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~

Related Tags