Python Program to Print Alphabets Using ASCII Value

In this article, we will learn about python program to print alphabets using ASCII value with examples.

Getting Started

Out task is to print alphabets in either lowercase or uppercase using their ASCII values.

As we already know,

  • ASCII value of lowercase alphabets ranges from 97 to 122.
  • ASCII value of uppercase alphabets ranges from 65 to 90.

Given above facts, alphabets can be printed in below ways –

Print Alphabets using ASCII value using for Loop

Steps to print alphabets using for Loop –

  • Run a for loop from 97 to 122 for lowercase or from 65 to 90 for uppercase. We can use built-in function range() for this purpose.
  • At each iteration, get corresponding character using chr() function.
  • Once we have character, print it using print() function.
  • Print Alphabets in Lowercase

    ASCII values of lowercase alphabets ranges from 97 to 122. So, we need to run a for loop from 97 to 122.

    for ch in range(97, 123):
        print("ASCII value: " + str(ch) + ", Character: ", chr(ch))
    

    Output:

    ASCII value: 97, Character:  a
    ASCII value: 98, Character:  b
    ASCII value: 99, Character:  c
    ASCII value: 100, Character:  d
    ASCII value: 101, Character:  e
    ASCII value: 102, Character:  f
    ASCII value: 103, Character:  g
    ASCII value: 104, Character:  h
    ASCII value: 105, Character:  i
    ASCII value: 106, Character:  j
    ASCII value: 107, Character:  k
    ASCII value: 108, Character:  l
    ASCII value: 109, Character:  m
    ASCII value: 110, Character:  n
    ASCII value: 111, Character:  o
    ASCII value: 112, Character:  p
    ASCII value: 113, Character:  q
    ASCII value: 114, Character:  r
    ASCII value: 115, Character:  s
    ASCII value: 116, Character:  t
    ASCII value: 117, Character:  u
    ASCII value: 118, Character:  v
    ASCII value: 119, Character:  w
    ASCII value: 120, Character:  x
    ASCII value: 121, Character:  y
    ASCII value: 122, Character:  z
    
  • Print Alphabets in Uppercase

    ASCII values of uppercase alphabets ranges from 65 to 90. So, we need to run a for loop that ranges between same interval.

    for ch in range(65, 91):
        print("ASCII value: " + str(ch) + ", Character: ", chr(ch))
    

    Output:

    ASCII value: 65, Character:  A
    ASCII value: 66, Character:  B
    ASCII value: 67, Character:  C
    ASCII value: 68, Character:  D
    ASCII value: 69, Character:  E
    ASCII value: 70, Character:  F
    ASCII value: 71, Character:  G
    ASCII value: 72, Character:  H
    ASCII value: 73, Character:  I
    ASCII value: 74, Character:  J
    ASCII value: 75, Character:  K
    ASCII value: 76, Character:  L
    ASCII value: 77, Character:  M
    ASCII value: 78, Character:  N
    ASCII value: 79, Character:  O
    ASCII value: 80, Character:  P
    ASCII value: 81, Character:  Q
    ASCII value: 82, Character:  R
    ASCII value: 83, Character:  S
    ASCII value: 84, Character:  T
    ASCII value: 85, Character:  U
    ASCII value: 86, Character:  V
    ASCII value: 87, Character:  W
    ASCII value: 88, Character:  X
    ASCII value: 89, Character:  Y
    ASCII value: 90, Character:  Z
    

Print Alphabets using ASCII value using while Loop

Steps to print alphabets using while Loop in python –

  • Initialise a counter variable, ch, with ASCII value of first letter of alphabets. For lowercase, it is 97 and that of uppercase is 65.
  • Run a while loop until value of variable is last character of alphabets. For lowercase, it is 122 and that of uppercase is 90.
  • At each iteration, get the corresponding character using chr() function.
  • Then, print returned value using print() function.
  • After that increment the value of counter variable by 1.
  • Print Alphabets in Lowercase

    ASCII value of first and Last letters of alphabets in lowercase are 97 and 122 respectively.

    We can print alphabets in lowercase using while loop as shown below –

    ch = 97
    while(ch < 123):
        print("ASCII value: " + str(ch) + ", Character: ", chr(ch))
        ch = ch + 1
    

    Output:

    ASCII value: 97, Character:  a
    ASCII value: 98, Character:  b
    ASCII value: 99, Character:  c
    ASCII value: 100, Character:  d
    ASCII value: 101, Character:  e
    ASCII value: 102, Character:  f
    ASCII value: 103, Character:  g
    ASCII value: 104, Character:  h
    ASCII value: 105, Character:  i
    ASCII value: 106, Character:  j
    ASCII value: 107, Character:  k
    ASCII value: 108, Character:  l
    ASCII value: 109, Character:  m
    ASCII value: 110, Character:  n
    ASCII value: 111, Character:  o
    ASCII value: 112, Character:  p
    ASCII value: 113, Character:  q
    ASCII value: 114, Character:  r
    ASCII value: 115, Character:  s
    ASCII value: 116, Character:  t
    ASCII value: 117, Character:  u
    ASCII value: 118, Character:  v
    ASCII value: 119, Character:  w
    ASCII value: 120, Character:  x
    ASCII value: 121, Character:  y
    ASCII value: 122, Character:  z
    
  • Print Alphabets in Uppercase

    ASCII value of first and last letters of alphabets in uppercase are 65 and 90 respectively.

    We can print alphabets in uppercase using while loop as shown below –

     
    ch = 65
    while(ch < 91):
        print("ASCII value: " + str(ch) + ", Character: ", chr(ch))
        ch = ch + 1
    

    Output:

    ASCII value: 65, Character:  A
    ASCII value: 66, Character:  B
    ASCII value: 67, Character:  C
    ASCII value: 68, Character:  D
    ASCII value: 69, Character:  E
    ASCII value: 70, Character:  F
    ASCII value: 71, Character:  G
    ASCII value: 72, Character:  H
    ASCII value: 73, Character:  I
    ASCII value: 74, Character:  J
    ASCII value: 75, Character:  K
    ASCII value: 76, Character:  L
    ASCII value: 77, Character:  M
    ASCII value: 78, Character:  N
    ASCII value: 79, Character:  O
    ASCII value: 80, Character:  P
    ASCII value: 81, Character:  Q
    ASCII value: 82, Character:  R
    ASCII value: 83, Character:  S
    ASCII value: 84, Character:  T
    ASCII value: 85, Character:  U
    ASCII value: 86, Character:  V
    ASCII value: 87, Character:  W
    ASCII value: 88, Character:  X
    ASCII value: 89, Character:  Y
    ASCII value: 90, Character:  Z
    

That’s how we can write python program to print alphabets using ASCII value in lowercase or uppercase .

Reference: Official Doc

Leave a Reply