Examples of how to create a list of English alphabet letters (from A to Z) with python:
List of alphabet letters (lowercase)
To get a list of English alphabet letters with python, a straightforward solution is to use the library called string
import string
english_alphabet_string_lowercase = string.ascii_lowercase
english_alphabet_string_lowercase
gives
'abcdefghijklmnopqrstuvwxyz'
Then to transform the above string to a list, just use the list() function:
english_alphabet_string_lowercase_list = list(english_alphabet_string_lowercase)
print( english_alphabet_string_lowercase_list )
gives
['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']
Note: if you want to upper case a given letter, b for instance:
english_alphabet_string_lowercase_list[1]
'b'
a solution is to use the method upper():
english_alphabet_string_lowercase_list[1] = english_alphabet_string_lowercase_list[1].upper()
print( english_alphabet_string_lowercase_list )
gives
['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']
List of alphabet letters in (uppercase)
To create a list of alphabet letters in uppercase, we can use a list comprehension:
[l.upper() for l in english_alphabet_string_lowercase]
gives
['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']
or directly from the library sring:
english_alphabet_string_uppercase= string.ascii_uppercase
english_alphabet_string_uppercase
gives
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
then create a list using list() method:
english_alphabet_string_uppercase_list = list(english_alphabet_string_uppercase)
print( english_alphabet_string_uppercase_list )
gives
['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']
Note: same to transform a given letter from uppercase to lowercase
english_alphabet_string_uppercase_list[1]
B
english_alphabet_string_uppercase_list[1].lower()
b
List of lowercase and uppercase alphabet letters
alphabet = list(string.ascii_lowercase) + list(string.ascii_uppercase)
gives
['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', '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']
Example of application: create a simple coded message
Create a list of alphabet letters
a1 = list(string.ascii_lowercase) + list(string.ascii_uppercase)
print( alphabet )
gives
['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', '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']
Rotate elements of the previous list
from collections import deque
a2 = deque( a1 )
a2.rotate(2)
print( list( a2 ) )
gives
['Y', 'Z', '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', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X']
Create a secret dictionary
secret = {}
for letter, coded_letter in zip(a1,a2):
secret[letter] = coded_letter
and a function to encode a word:
def encoder_function(letter):
return secret[letter]
Now let's encode the word 'Hello':
s = 'Hello'
s = list(s)
print( encoder_function('a') )
''.join( list( map(encoder_function, s) ) )
gives
'Fcjjm'