Python Program to Display Characters from A to Z Using Loop

In this python tutorial, you will learn how to Display Characters from A to Z Using for loop, the chr and string function of the python programming language.

ASCII, abbreviation of American Standard Code For Information Interchange, a standard data-transmission code that is used by smaller and less-powerful computers to represent both textual data like letters, numbers, and punctuation marks and noninput-device commands like control characters.

How to Display Characters from A to Z Using Loop?

Let’s take a look at the first source code , here we use char function to display the alphabet where the for loop and the range are used to carry out the function.

RUN CODE SNIPPET
#Python Program to Display Characters from A to Z Using Loop using char function

print("The Lowercase of the alphabets: ")
for i in range(97,123):
    print(chr(i), end=" ")

print("\n")

print("The Uppercase of the alphabets: ")
for i in range(65,91):
    print(chr(i), end=" ")

OUTPUT:

The Lowercase of the 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

The Uppercase of the 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
  1. We start by displaying the statement "The Lowercase of the alphabets: ") using the print function, after which we declare a for loop where the range for the variable i is (97,123) , followed by a :.
  2. Using the print function and (chr(i), end=" ") condition we display the values of i between the range (97,123)
  3. We declare a print function with ("\n ") which prints a line without any arguments, that results a new line.
  4. Next we display the statement "The Uppercase of the alphabets: ") using the print function, after which we declare a for loop where the range for the variable i is (65,91) , followed by a :.
  5. Using the print function and (chr(i), end=" ") condition we display the values of i between the range (65,91)
  6. We use the ascii codes (97,123) as ’97’ is the ascii code of small letter ‘a’ and  ‘123’ is the ascii code of small letter ‘z’. Similarly (65,91) are the ascii codes of the capital letter ‘A’ and ‘Z’ respectively.

Let’s take a look at the second source code , here we use string module to display the alphabet where the for loop and ascii string are used to carry out the function.

RUN CODE SNIPPET
#Python Program to Display Characters from A to Z Using Loop using string module

import string

print("The Lowercase of the alphabets: ")
for i in string.ascii_lowercase:
    print(i, end=" ")

print("\n")

print("The Uppercase of the alphabets: ")
for i in string.ascii_uppercase:
    print(i, end=" ")

OUTPUT:

The Lowercase of the 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

The Uppercase of the 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
  1. We start the code with import string , after which we display the statement "The Lowercase of the alphabets: ") using the print function, after which we declare a for loop where the function string.ascii_lowercase will return the Lowercase letter, followed by a :.
  2. Using the print function and (i, end=" ") condition we display the values of i.
  3. We declare a print function with ("\n ") which prints a line without any arguments, that results a new line.
  4. Next we display the statement "The Uppercase of the alphabets: ") using the print function, after which we declare a for loop where the function string.ascii_uppercase will return the Uppercase letter, followed by a :.
  5. Using the print function and (i, end=" ") condition we display the values of i.

NOTE:

  • A For loop runs a block of code until the loop has iterated over every item in a condition, that is it helps to reduce any repetition in the code because it executes the same operation multiple times.
  • The Range() function is used to generate a sequence of numbers, where the range() function is commonly used in for looping functions.
  • The chr() method returns a string representing a character whose Unicode code point is an integer, this method takes only one integer as argument.
  • The ascii_lowercase and ascii_uppercase is a pre-initialized string used as string constant which returns the Lowercase and the Uppercase of the Alphabet.
  • The import string conducts the process of importing where one module gains access to the code in another module.
  • The end=’ ‘ in python is used to place a space after the displayed string instead of a newline.
  • The print statement/string to be displayed in enclosed in double quotes.
  • The input() function allows a user to insert a value into a program, it returns a integer value.
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...