In this article, we will learn about python program to print alphabets till entered character. For example,
If user enters N, output should be –
A B C D E F G H I J K L M N
If user enters r, output should be –
a b c d e f g h i j k l m n o p q r
Note that when user enters letter in lowercase, output is in lowercase. When user enters letter in uppercase, output is in uppercase.
Getting Started
We can do it in multiple ways –
Using Simple Comparison
We can simply compare the entered character using built-in methods islower() or isupper() and print all letters till entered character.
Pseudo Algorithm:
- Check if entered character is in lowercase or uppercase using islower() and isupper() method.
- Then, print each character starting from first character till we reach entered character.
def printLetters(ch): if len(ch) != 1: print("Invalid letter") else : chASCIIValue = ord(ch) print("Printing alphabet till: ", ch) if ch.islower(): start = 97 while start <= chASCIIValue: print(chr(start), end = " ") start = start + 1 elif ch.isupper(): start = 65 while start <= chASCIIValue: print(chr(start), end = " ") start = start + 1 a = input('Enter a letter') printLetters(a)
Output:
Enter a letter h Printing alphabet till: h a b c d e f g h
In above program,
- At first, we get any character entered by user and passed it to printLetters() method.
- Then, we check if length of entered character is more than 1 or not. If yes, it means we have entered an invalid character. If No, it means we have entered a valid character.
- Then, we get ASCII value of entered character and check if it in lowercase or uppercase.
- Then, we print all letters using while loop till we reach entered character.
Using Comparison with ASCII Value
In above program, we can use ASCII value to print till entered character in alphabets as shown below –
def printLetters(ch): if len(ch) != 1: print("Invalid letter") else : chASCIIValue = ord(ch) print("Printing alphabet till: ", ch) if ch >= 97 and ch <= 122: start = 97 while start <= chASCIIValue: print(chr(start), end = " ") start = start + 1 elif ch >= 65 and ch <= 90: start = 65 while start <= chASCIIValue: print(chr(start), end = " ") start = start + 1 a = input('Enter a letter') printLetters(a)
Output:
Enter a letter H Printing alphabet till: H A B C D E F G H
In above program,
We have directly compared entered character with ASCII value of alphabets. Other things are same as above program.
Using string constant – ascii_uppercase
We can use string constant – ascii_uppercase in our program to print all alphabets till entered character.
Pseudo Algorithm
- Get input from user. Check if it valid character or not.
- If yes, find if it is in lowercase or uppercase.
- Then, find out how many characters do we have to print.
- Get all the characters till entered value using ascii_uppercase
- Print returned value using print() method.
import string def printLetters(ch): if len(ch) != 1: print("Invalid letter") else : chASCIIValue = ord(ch) print("Printing alphabet till: ", ch) lowercaseASCIIValue = 97 uppercaseASCIIValue = 65 N = chASCIIValue - lowercaseASCIIValue + 1 res = "" if(N < 0): # check if letters are in uppercase N = chASCIIValue - uppercaseASCIIValue + 1 res = string.ascii_uppercase[:N] else: # letters are in lowercase res = string.ascii_lowercase[:N] print(str(res)) a = input('Enter a letter') printLetters(a)
Output:
Enter a letter X Printing alphabet till: X ABCDEFGHIJKLMNOPQRSTUVWX
That’s how we can write python program to print alphabets till entered character.
Reference – Official doc