In this article, we will learn about python program to print N letters of alphabets in uppercase with examples.
Getting Started
The task is to print N letters of alphabets in uppercase in python. For example,
If N = 6, output is –
A B C D E F
If N = 20, output is –
A B C D E F G H I J K L M N O P Q R S T
We can do above task –
Print N Letters of Alphabets in Uppercase Using For Loop
Python Program to print N letters of alphabets in uppercase using for loop –
n = int(input("Enter number of letters to print"))
for i in range(n):
print(chr(65 + i), end = " ")
Output:
Enter number of letters to print 20 A B C D E F G H I J K L M N O P Q R S T
65 is ASCII value of A. chr() function accepts integer value as parameter and returns corresponding character.
Print N Letters of Alphabets in Uppercase Using While Loop
Pseudo Algorithm
- Initialise a counter variable, i.e. i = 0, to be used with while Loop.
- Using ASCII value of character, we print character using chr() function.
- Increment counter variable by 1.
print n letters of alphabets in uppercase using while loop –
n = int(input("Enter number of letters to print"))
i = 0
while (i < n):
print(chr(65 + i), end = " ")
i = i + 1
Output:
Enter number of letters to print 19 A B C D E F G H I J K L M N O P Q R S
Print N Letters of Alphabets Using String Constant – ascii_uppercase
Using string constant ascii_uppercase, we can retrieve alphabets in uppercase.
import string
n = int (input("Enter number of letters to print"))
res = string.ascii_uppercase[:n]
print(str(res))
Output:
Enter number of letters to print 18 ABCDEFGHIJKLMNOPQR
ascii_uppercase returns alphabets in uppercase. We can also specify number of letters we want in constant.
Reference – Official Doc