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
- We start by displaying the statement
"The Lowercase of the alphabets: ")
using theprint
function, after which we declare afor
loop where therange
for the variablei
is(97,123)
, followed by a:
. - Using the
print
function and(chr(i), end=" ")
condition we display the values ofi
between the range(97,123)
- We declare aÂ
print
function with("\n ")
which prints a line without any arguments, that results a new line. - Next we display the statement
"The Uppercase of the alphabets: ")
using theprint
function, after which we declare afor
loop where therange
for the variablei
is(65,91)
, followed by a:
. - Using the
print
function and(chr(i), end=" ")
condition we display the values ofi
between the range(65,91)
- 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
- We start the code with
import string
, after which we display the statement"The Lowercase of the alphabets: ")
using theprint
function, after which we declare afor
loop where the functionstring.ascii_lowercase
will return the Lowercase letter, followed by a:
. - Using the
print
function and(i, end=" ")
condition we display the values ofi
. - We declare aÂ
print
function with("\n ")
which prints a line without any arguments, that results a new line. - Next we display the statement
"The Uppercase of the alphabets: ")
using theprint
function, after which we declare afor
loop where the functionstring.ascii_uppercase
will return the Uppercase letter, followed by a:
. - Using the
print
function and(i, end=" ")
condition we display the values ofi
.
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.