Python - Print alphabets till N


Python language provides the user with versatile roles ranging from basic calculations to complex ones. The alphabets get printed up to the ‘N’ value as mentioned by the user. When we say printing the alphabet up to some range seems to be a simple task, but when creating a program, involves some tedious processes. The Python methods use various functionalities to print the alphabet till N. This algorithm acts as a base for machine learning technology.

Print alphabets till N

In the English language there are twenty-six alphabets in total, when the range, N is mentioned as 9, then up to “i” is printed from the Python program. Like a, b, c, d, e, f, g, h, j and i is the output printed when the range is mentioned as 9.

Approach

  • Approach 1: Using the string module

  • Approach 2: Using the iteration method

  • Approach 3: Using the join function

Approach 1: Python Program to Print the alphabet till N using the string module

The string module is exclusively used for printing the range of alphabets using the “N” value. The module provides the user with predefined constants composed of ASCII characters. When the lowercase function is used, it provides the alphabet in lowercase letters.

Algorithm

  • Step 1 − The string module is imported.

  • Step 2 − The input variable is initialized in the form of an integer data type.

  • Step 3 − The for loop is used to iterate through the range of values as mentioned by the user from 0 to string1.

  • Step 4 − The ascii_lowercase() function from the string module, to display the output in lowercase.

  • Step 5 − The colon will truncate through the list easily.

  • Step 6 − The list of alphabets is printed vertically.

Example

#importing the string module
import string

#The range needs to be mentioned here as “N” 
string1 = 5
#for loop is used to iterate through the given input
#the ascii_lowercase function is used to print the result in lowercase
for alphabet in string.ascii_uppercase[:string1]:
#Print statement will return the list of alphabets according to the range value
	print(alphabet)

Output

A
B
C
D
E

Approach 2: Python Program to Print the alphabet till N using the iteration method

The alphabets are printed without the use of any built-in modules, rather the for loop is used. The for loop will iterate through the range of values as given by the user.

Algorithm

  • Step 1 − The range values are set.

  • Step 2 − The for loop will iterate from the 0 to the value mentioned by the user, in this case N=6.

  • Step 3 − The chr() function is used to convert each value into its equivalent ASCII character and then add 97.

  • Step 4 − The value”97” is added, as the lowercase letters start from it.

  • Step 5 − The print function returns the range of alphabets.

Example

#initializing the input value
range_value = 5
#for loop used to iterate through the values
#range inbuilt function iterates through the value
for a in range(range_value):
#print the range of alphabets using the print statement
	print(chr(a+97))

Output

a
b
c
d
e

Approach 3: Python Program to Print the alphabet till N using the join function

The string module is used to deal with printing the string values. Along with it, the join() function is used to combine all the letters.

Algorithm

  • Step 1 − The required module to use the join function is a string need to be imported.

  • Step 2 − Set the value of string1.

  • Step 3 − The newline character is declared to print the alphabet in separate lines.

  • Step 4 − The join function combines all the lowercase letters of the English alphabet up to the range “N”.

  • Step 5 − The print statement returns the string value in new lines up to the N value.

Example

#initializing the string module to hold the join function
import string

#The output is printed from the range mentioned in an integer value
string1 = 6
#print statement returns the result
print('\n'.join(string.ascii_lowercase[:string1]))

Output

a
b
c
d
e
f

Conclusion

The strings module which is a predefined library to use the ASCII alphabets is explained in two approaches. The above approaches can be performed to print the alphabet in uppercase letters also, by using the uppercase() function. By this, the programmer can easily understand the implementation process of strings up to a range.

Updated on: 04-Sep-2023

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements