How to print alphabets in python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, while working with Python, we can have a problem in which we need to print the specific number of alphabets in order. This can have application in school-level programming. Let’s discuss certain ways in which this problem can be solved. 

    Method #1 : Using loop + chr()

    This is brute force way to perform this task. In this, we iterate the elements till which we need to print and concatenate the strings accordingly after conversion to the character using chr(). 

    Python3

    N = 20

    print("Number of elements required : " + str(N))

    res = ""

    for idx in range(97, 97 + N):

           res = res + chr(idx)

    print("Alphabets till N are : " + str(res))

    Output : 

    Number of elements required : 20
    Alphabets till N are : abcdefghijklmnopqrst

    Method #2: Using string.ascii_lowercase + slicing:

    Combination of the above functionalities can also be used to perform this task. In this, we use an inbuilt function to get extract the lowercase string and extract the N characters using slicing. 

    Python3

    import string

    N = 20

    print(& quot

           Number of elements required : & quot

           + str(N))

    res = string.ascii_lowercase[:N]

    print(& quot

           Alphabets till N are : & quot

           + str(res))

    Output : 

    Number of elements required : 20
    Alphabets till N are : abcdefghijklmnopqrst

    Method #3 : Using ord() and chr() functions

    The Python ord() function converts a character into an integer that represents the Unicode code of the character. Similarly, the chr() function converts a Unicode code character into the corresponding string.

    Python3

    N = 20

    print("Number of elements required : " + str(N))

    s=""

    x=ord('a')+N-1

    y=ord('a')

    while(y<=x):

        s+=chr(y)

        y+=1

    print("Alphabets till N are : " + s)

    Output

    Number of elements required : 20
    Alphabets till N are : abcdefghijklmnopqrst


    Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)

    Python Basic - 1: Exercise-114 with Solution

    Write a Python program to print letters from the English alphabet from a-z and A-Z.

    Sample Solution:

    Python Code:

    import string
    print("Alphabet from a-z:")
    for letter in string.ascii_lowercase:
       print(letter, end =" ")
    print("\nAlphabet from A-Z:")
    for letter in string.ascii_uppercase:
       print(letter, end =" ")
    

    Sample Output:

    Alphabet from a-z:
    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 
    Alphabet from A-Z:
    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 
    

    Pictorial Presentation:

    Flowchart:

    How to print alphabets in python

    Python Code Editor:

    Have another way to solve this solution? Contribute your code (and comments) through Disqus.

    Previous: Write a Python program to reverse all the words which have even length.
    Next: Write a Python program to generate and prints a list of numbers from 1 to 10.

    What is the difficulty level of this exercise?

    Test your Programming skills with w3resource's quiz.

    Python: Tips of the Day

    One Liner For Loops:

    resultA, resultB = get_result()get_result() can return ('a', 1) which is a tuple
    


    • Exercises: Weekly Top 16 Most Popular Topics
    • SQL Exercises, Practice, Solution - JOINS
    • SQL Exercises, Practice, Solution - SUBQUERIES
    • JavaScript basic - Exercises, Practice, Solution
    • Java Array: Exercises, Practice, Solution
    • C Programming Exercises, Practice, Solution : Conditional Statement
    • HR Database - SORT FILTER: Exercises, Practice, Solution
    • C Programming Exercises, Practice, Solution : String
    • Python Data Types: Dictionary - Exercises, Practice, Solution
    • Python Programming Puzzles - Exercises, Practice, Solution
    • C++ Array: Exercises, Practice, Solution
    • JavaScript conditional statements and loops - Exercises, Practice, Solution
    • C# Sharp Basic Algorithm: Exercises, Practice, Solution
    • Python Lambda - Exercises, Practice, Solution
    • Python Pandas DataFrame: Exercises, Practice, Solution
    • Conversion Tools
    • JavaScript: HTML Form Validation


    How do you print alphabet letters in Python?

    Python: Print letters from the English alphabet from a-z and A-Z.
    Sample Solution:.
    Python Code: import string print("Alphabet from a-z:") for letter in string.ascii_lowercase: print(letter, end =" ") print("\nAlphabet from A-Z:") for letter in string.ascii_uppercase: print(letter, end =" ") ... .
    Pictorial Presentation:.

    How print A to Z in Python?

    “print all alphabets from a to z in python” Code Answer.
    alphabet = [];.
    # ord represent the character unicode code, A to 65..
    # chr convert 65 to A..
    #variable alphabet get all value..
    for i in range(ord('A'), ord('Z') + 1):.
    alphabet. append(chr(i)).
    print(alphabet).
    #ganesh..