Initializing a list with alphabets can be useful for various tasks. Here's how you can initialize a list with the English alphabets in Python:
string module:The string module provides constants ascii_lowercase and ascii_uppercase which represent the lowercase and uppercase English alphabets respectively.
import string lowercase_alphabets = list(string.ascii_lowercase) print(lowercase_alphabets) uppercase_alphabets = list(string.ascii_uppercase) print(uppercase_alphabets)
chr():The ASCII values of lowercase alphabets are from 97 (a) to 122 (z). For uppercase, they are from 65 (A) to 90 (Z). You can use this information with the chr() function:
lowercase_alphabets = [chr(i) for i in range(97, 123)] print(lowercase_alphabets) uppercase_alphabets = [chr(i) for i in range(65, 91)] print(uppercase_alphabets)
For short lists like alphabets, you can directly assign them:
lowercase_alphabets = ['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'] print(lowercase_alphabets) uppercase_alphabets = ['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'] print(uppercase_alphabets)
map():You can combine map() with the chr() function:
lowercase_alphabets = list(map(chr, range(97, 123))) print(lowercase_alphabets) uppercase_alphabets = list(map(chr, range(65, 91))) print(uppercase_alphabets)
string module approach would be more readable and easier for others to understand your intent.By using any of the above methods, you can easily initialize a list with English alphabets in Python.
jsonschema internet-explorer-8 web-console checksum mule-esb wsimport quicksort persistent protorpc masstransit