In this article, we will learn about python program to print ASCII values of alphabets. In other words, we will learn to print ASCII values of all letters of alphabets from a to z to A to Z i.e. in lowercase and uppercase both.
Getting Started
The task is to print ASCII value of alphabets in lowercase or uppercase.
Alphabets in 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
Alphabets in Uppercase –
A B C D E F G H I J K L M O P N Q R S T U V W X Y Z
We have to print ASCII values of each letters of alphabets shown above.
We can do so in below ways –
Using Hardcoded String Values
We know that there are total 26 letters of alphabets. It won’t change. So, we can use below algorithm to print ASCII values of each letter in alphabet
Pseudo Algorithm
- Store the alphabets as string in a variable.
- Iterate over string.
- In each iteration, get ASCII value using ord() function and print it using print() function.
-
Print ASCII values of Alphabets in Lowercase
Store all lowercase alphabets in variable and print ascii values as shown below –
alphabets = "abcdefghijklmnopqrstuvwxyz" for ch in alphabets: print("Character: " + ch + ", ASCII Value: ", ord(ch))
Output:
Character: a, ASCII Value: 97 Character: b, ASCII Value: 98 Character: c, ASCII Value: 99 Character: d, ASCII Value: 100 Character: e, ASCII Value: 101 Character: f, ASCII Value: 102 Character: g, ASCII Value: 103 Character: h, ASCII Value: 104 Character: i, ASCII Value: 105 Character: j, ASCII Value: 106 Character: k, ASCII Value: 107 Character: l, ASCII Value: 108 Character: m, ASCII Value: 109 Character: n, ASCII Value: 110 Character: o, ASCII Value: 111 Character: p, ASCII Value: 112 Character: q, ASCII Value: 113 Character: r, ASCII Value: 114 Character: s, ASCII Value: 115 Character: t, ASCII Value: 116 Character: u, ASCII Value: 117 Character: v, ASCII Value: 118 Character: w, ASCII Value: 119 Character: x, ASCII Value: 120 Character: y, ASCII Value: 121 Character: z, ASCII Value: 122
-
Print ASCII values of Alphabets in Uppercase
Like lowercase alphabets, store uppercase alphabets in variable and iterate over it. For example,
alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for ch in alphabets: print("Character: " + ch + ", ASCII Value: ", ord(ch))
Output:
Character: A, ASCII Value: 65 Character: B, ASCII Value: 66 Character: C, ASCII Value: 67 Character: D, ASCII Value: 68 Character: E, ASCII Value: 69 Character: F, ASCII Value: 70 Character: G, ASCII Value: 71 Character: H, ASCII Value: 72 Character: I, ASCII Value: 73 Character: J, ASCII Value: 74 Character: K, ASCII Value: 75 Character: L, ASCII Value: 76 Character: M, ASCII Value: 77 Character: N, ASCII Value: 78 Character: O, ASCII Value: 79 Character: P, ASCII Value: 80 Character: Q, ASCII Value: 81 Character: R, ASCII Value: 82 Character: S, ASCII Value: 83 Character: T, ASCII Value: 84 Character: U, ASCII Value: 85 Character: V, ASCII Value: 86 Character: W, ASCII Value: 87 Character: X, ASCII Value: 88 Character: Y, ASCII Value: 89 Character: Z, ASCII Value: 90
Using string.ascii_lowercase or string.ascii_uppercase
In previous codes, we hardcoded all alphabets and stored in a variable. If we don’t want to hardcode it, we can either string constants – string.ascii_lowercase or string.ascii_uppercase to get alphabets in lowercase and uppercase respectively.
Pseudo Algorithm
- Store value returned by string.ascii_lowercase or string.ascii_uppercase in a variable.
- Then, run a for loop on returned values.
- In each iteration, get ASCII value of character using ord() function and print it using print() function.
-
Print ASCII values using string.ascii_lowercase
string.ascii_lowercase returns alphabets in lowercase. So, we can print ascii values of alphabets as shown below –
import string alphabets = string.ascii_lowercase for ch in alphabets: print("Character: " + ch + ", ASCII Value: ", ord(ch))
Output:
Character: a, ASCII Value: 97 Character: b, ASCII Value: 98 Character: c, ASCII Value: 99 Character: d, ASCII Value: 100 Character: e, ASCII Value: 101 Character: f, ASCII Value: 102 Character: g, ASCII Value: 103 Character: h, ASCII Value: 104 Character: i, ASCII Value: 105 Character: j, ASCII Value: 106 Character: k, ASCII Value: 107 Character: l, ASCII Value: 108 Character: m, ASCII Value: 109 Character: n, ASCII Value: 110 Character: o, ASCII Value: 111 Character: p, ASCII Value: 112 Character: q, ASCII Value: 113 Character: r, ASCII Value: 114 Character: s, ASCII Value: 115 Character: t, ASCII Value: 116 Character: u, ASCII Value: 117 Character: v, ASCII Value: 118 Character: w, ASCII Value: 119 Character: x, ASCII Value: 120 Character: y, ASCII Value: 121 Character: z, ASCII Value: 122
-
Print ASCII values using string.ascii_uppercase
string.ascii_uppercase returns alphabets in Uppercase. So, we can write python program to print ascii values of alphabets as shown below –
import string alphabets = string.ascii_uppercase for ch in alphabets: print("Character: " + ch + ", ASCII Value: ", ord(ch))
Output:
Character: A, ASCII Value: 65 Character: B, ASCII Value: 66 Character: C, ASCII Value: 67 Character: D, ASCII Value: 68 Character: E, ASCII Value: 69 Character: F, ASCII Value: 70 Character: G, ASCII Value: 71 Character: H, ASCII Value: 72 Character: I, ASCII Value: 73 Character: J, ASCII Value: 74 Character: K, ASCII Value: 75 Character: L, ASCII Value: 76 Character: M, ASCII Value: 77 Character: N, ASCII Value: 78 Character: O, ASCII Value: 79 Character: P, ASCII Value: 80 Character: Q, ASCII Value: 81 Character: R, ASCII Value: 82 Character: S, ASCII Value: 83 Character: T, ASCII Value: 84 Character: U, ASCII Value: 85 Character: V, ASCII Value: 86 Character: W, ASCII Value: 87 Character: X, ASCII Value: 88 Character: Y, ASCII Value: 89 Character: Z, ASCII Value: 90
That’s how we can write python program to print ASCII values of alphabets.
Reference: Official Doc
Learn more about python at – https://tutorialwing.com/python-tutorial/