Python program to print alphabetical pattern

In this tutorial, you are going to learn how to print various alphabetic pattern in Python. Alphabetical (A-Z) pattern is a series of alphabet which forms a pattern or any shape like triangle, square, rhombus etc. These patterns are created with the help of nested for loop. To create the alphabetic pattern, you should know that how to convert the ASCII value into the ASCII character.

chr(): This function is used to convert the ASCII value into the ASCII character.

ASCII characters                                        ASCII value

A-Z                                                                                               [65-90]
a-z                                                                                                [97-122]
0-9                                                                                                [48-57]
Special symbols                                                         [0-47,58-64,91-96,123-127]
         

Print Alphabetic Pattern 1: Python

  1. During the first iteration of the outer loop, the i becomes 65 and it goes into the inner loop
  2. The inner loop will work for the range(65,i+1) {i.e., (65,66), (65,67), (65,68), (65,69), (65, 70) for five consecutive iterations of the outer loop}
  3. The inner loop will print the ASCII character of j by converting the ASCII value.
  4. Print() takes the pointer in the next line.
  5. The outer loop will continue until i become 69 till repeat all the above steps to print the pattern.
# outer loop
for i in range (65,70):
    # inner loop
    for j in range(65,i+1):
        print(chr(j),end="")
    print()

Output:-

A
AB
ABC
ABCD
ABCDE

Print Alphabetic pattern 2: Python

This pattern is similar to the above pattern in working. The only difference is that, the inner loop is printing the ASCII character of i instead of j.

# outer loop
for i in range (65,70):
    # inner loop
    for j in range(65,i+1):
        print(chr(i),end="")
    print()

Output:-

A
BB
CCC
DDDD
EEEEE

Alphabetical pattern 3:

This pattern is similar to the above pattern. The only difference is that, we are storing the value of i in a variable and printing that variable again and again by incrementing it side by side to form the pattern.

# Outer loop
for i in range(65,70):
    k=i
    # Inner loop
    for j in range(65,i+1):
        print(chr(k),end="")
        k=k+1
    print()

Output:-

A
BC
CDE
DEFG
EFGHI

Alphabetic pattern 4:

This pattern is used when you have to print the specific string as a pattern.

  1. The specific string is input in the str whose pattern is to be created.
  2. Set the range of the outer and inner loop according to the string length.
  3.  Print the str character as the pattern with the help of indexing using j.
  4. Print() will takes the pointer in the next line. The first iteration is complete.
  5. The outer loop will continue until i become 6 till repeat all the above steps to print the pattern.
str= "APOORVA"  # string
# Outer loop
for i in range(0,7):
    # inner loop
    for j in range(0,i+1):
        print(str[j],end="")
    print()

Output:-

A
AP
APO
APOO
APOOR
APOORV
APOORVA

Alphabetical pattern 5:

  1. During the first iteration of the outer loop, then i has the value 65 and goes into the inner loop.
  2. The inner loop will work for the range (i,64,-1) which means that it will work in the decrement order and print the pattern like this DCBA.
  3. Print the j using chr() function to get the ASCII character.
  4.  Print() will takes the pointer in the next line. The first iteration is complete.
  5. The outer loop will continue until i become 69 till repeat all the above steps to print the pattern.
# Outer loop
for i in range(65,70):
    # Inner loop
    for j in range(i,64,-1):
        print(chr(j),end="")
    print()

Output:-

A
BA
CBA
DCBA
EDCBA

Alphabetic pattern 6:

This pattern is called pyramid pattern. It has three inner loops.

  1. Set a variable according to the white space of the left side of the pyramid.
  2. The outer loop will work for the range (65,70).
  3. Then it goes to the inner loop 1. This loop will print the white space. The white space will decrement line by line as the for loop range is set in negative.
  4. The inner loop 2 will print the alphabetic pattern on the left side.
         A
        AB
       ABC
      ABCD
     ABCDE
  5. The inner loop 3 will print the pattern on the right side with the first line blank and other line has the alphabetic pattern.
    A
    AB
    ABC
    ABCD
  6. By combining all the inner loops the pattern will be formed.
m=6   
# Outer loop
for i in range(65,70):
    m=m-1
    # Inner loop 1 
    for j in range(m,1,-1):
        print(" ",end="")
    # Inner loop 2
    for k in range(65,i+1):
        print(chr(k),end="")
    # Inner loop 3
    for n in range(65,i):
        print(chr(n),end="")
    print()

Output:-

    A
   ABA
  ABCAB
 ABCDABC
ABCDEABCD

Alphabetical pattern 6:

  1. During the first iteration of the outer loop, then i has the value 65 and goes into the inner loop.
  2. The inner loop will work for the range (i,64,-1) which means that it will work in the decrement order and print the pattern like this DCBA.
  3. Print the j using chr() function to get the ASCII character.
  4. Print() will takes the pointer in the next line. The first iteration is complete.
  5. The outer loop will continue until i become 69 untill repeat all the above steps to print the pattern.
# Outer loop
for i in range(65,70):
    # Inner loop
    for j in range(i,64,-1):
        print(chr(j),end="")
    print()

Output:-

A
BA
CBA
DCBA
EDCBA

Star(asterisk) pattern in Python

41 responses to “Python program to print alphabetical pattern”

  1. rads says:

    pls tell me how to generate alpha pattern using any other option, not ascii values

  2. Apoorva Gupta says:

    You can generate the pattern without using Ascii values with the help of below code:
    from string import ascii_uppercase
    for i in range(1,7):
    print(“”. join(ascii_uppercase[:i]))
    The ascii_uppercase function from string package will convert the value of i into the capital alphabets. The ascii_uppercase contains the alphabets from [A-Z].
    You, can also use the similar functions like
    ascii_lowercase that contains alphabets from [a-z].
    Or ascii_letters function that contains alphabets from [a-zA-Z].

  3. Robin singh says:

    Hey, How do you print this pattern?

    APQR
    ABQR
    ABCR
    ABCD

    Thanks!

    • Rajat goel says:

      for i in range(4):
      for j in range(4):
      if(i<j):
      print(chr(65+14+j), end=" ")
      else :
      print(chr(65+j), end=" ")
      print()

  4. yaksha says:

    plz print this pattern
    A
    BAB
    CBABC
    DCBABCD
    EDCBABCDE

    • Sanjay says:

      for i in range(65,70):
      for j in range(i,64,-1):
      print(chr(j),end=” “)
      for n in range(66,i+1):
      print(chr(n),end=” “)
      print()

  5. Apoorva Gupta says:

    You can use this code to produce the above pattern:
    m=5
    for i in range (65,70):
        m=m-1
        for j in range(m, 0,-1):
            print(” “, end=””)
        for k in range(i, 65, -1):
            print(chr(k),end=””)
        for n in range(65,i+1):
            print(chr(n), end=””)
        print()

    • Priyanshu Garg says:

      U can use this code also
      n=5
      For u in range(n):
      p=65
      for j in range(i+1):
      print(chr(p+i), end=(” “))
      p=66
      for k in range(i) :
      print(chr(p), end=(” “))
      p+=1
      print()

  6. Ajay says:

    How to generate this pattern ?
    ——–e——–
    ——e-d-e——
    —-e-d-c-d-e—-
    –e-d-c-b-c-d-e–
    e-d-c-b-a-b-c-d-e
    –e-d-c-b-c-d-e–
    —-e-d-c-d-e—-
    ——e-d-e——
    ——–e——–

  7. arjun c says:

    1
    1 D
    1 D O
    1 D O D
    1 D O D O

    can anyone help me in printing this pattern?

    • Apoorva Gupta says:

      l=[1,’D’,’O’,’D’,’O’]
      for i in range(0,5):
      for j in l[0:i+1]:
      print (j,end=” “)
      print()
      Output:-
      1
      1 D
      1 D O
      1 D O D
      1 D O D O

  8. Ratchanyaa Soundararajan says:

    Help me to print this pattern
    a b c d
    e f g
    h i
    j

    • Apoorva Gupta says:

      a=97
      for i in range(4,0,-1):
      for j in range(97,97+i):
      print(chr(a),end=” “)
      a=a+1
      print()
      Output:-
      a b c d
      e f g
      h i
      j

  9. Shashank Srivastava says:

    Input=2
    B B B
    B A B
    B B B
    program for this?

    • vicky says:

      Below Code will give you the required output
      x = int(input(“Enter Number Between 2 and 26 : “))
      d = {}
      ch = 65
      for i in range(1,27):
      d[i] = chr(ch)
      ch+=1
      for i in range(1,4):
      for j in range(1,4):
      if i == 2 and j == 2:
      print(d[x-1],end=””)
      else:
      print(d[x],end=””)
      print()

    • Shreya goyal says:

      for i in range(3):
      for j in range(3):
      if (i & j)==1:
      print(chr(65),end=””)
      else:
      print(chr(65+1),end=””)
      print ()
      Output:-
      BBB
      BAB
      BBB

  10. Anuraag Singh says:

    How do I encode
    A
    BC
    DEF
    GHIJ
    GHIJ
    DEF
    BC
    A in python

    • vicky says:

      Below code will print above pattern:
      for i in range(0,4):
      for j in range(0,i+1):
      print(chr(k),end=””)
      k+=1
      print()
      for i in range(4,0,-1):
      for j in range(i):
      print(chr(l),end=””)
      l+=1
      print()
      l-=t
      t-=2

  11. Saikat says:

    ——–e——–
    ——e-d-e——
    —-e-d-c-d-e—-
    –e-d-c-b-c-d-e–
    e-d-c-b-a-b-c-d-e
    –e-d-c-b-c-d-e–
    —-e-d-c-d-e—-
    ——e-d-e——
    ——–e——–

    • Spark_01 says:

      n=int(input())
      if n==0:
      print()
      elif n==1:
      print(‘a’)
      else:
      s=’abcdefghijklmnopqrstuvwxyz’
      a=s[0:n][::-1]
      l=[]
      for i in range(n):
      b=a[0:i+1]
      c=list(b)
      c=’-‘.join(c)
      l.append(c)
      x=s[1:n][::-1]
      l1=[]
      for i in range(n):
      d=x[0:i][::-1]
      d=list(d)
      t=’-‘.join(d)
      t=’-‘+t
      l1.append(t)
      for i in range(n):
      print(l[i].rjust((n*2)-1,’-‘),end=”)
      print(l1[i].ljust((n * 2) – 2, ‘-‘))
      l.reverse()
      l.remove(l[0])
      l1.reverse()
      l1.remove(l1[0])
      for i in range(n-1):
      print(l[i].rjust((n*2)-1,’-‘),end=”)
      print(l1[i].ljust((n * 2) – 2, ‘-‘))

  12. Aaryan says:

    If the output is:
    a
    Bc
    DeF
    gHiJ
    kLmNo
    Then the program is ?

  13. Deepankshu Singh says:

    Arnab be like – kuch bhii…
    Today is my cs exam, so wanted to see patterns, very nice content.

  14. Nancy says:

    Write a python program to print the following pattern:
    A B C D
    E F G
    H I
    J

  15. Pranav sai says:

    Pls help me with this program
    A
    B C
    D E F
    G H I J

  16. narasimha says:

    pls help me for this pattern
    A
    B B
    C C
    D D
    E E
    D D
    C C
    B B
    A

  17. Ashi says:

    Maam please help me in this pattern program.
    P
    P Y
    P Y T
    P Y T H
    P Y T H O
    P Y T H O N

  18. Yogesh shrivastav says:

    string=”PYTHON”
    k=0
    for x in range(len(string)+1):
    print(string[k:x])

  19. Shreya goyal says:

    str=[‘p’,’y’,’t’,’h’,’o’,’n’]
    for i in range(6):
    for j in range(0,i+1):
    print(str[j],end=” “)
    print()

  20. piyush says:

    how to reverse the alphabetic pattern no. 6
    A
    ABA
    ABCAB
    ABCDABC
    ABCDEABCD
    in reverse piramid

  21. anu says:

    For pattern like
    A
    B B
    C C
    D D
    E E
    D D
    C C
    B B
    A

  22. newga hetrio says:

    mam plz help me to print this pattern
    A
    B
    C
    D E F
    G H I
    J
    K L M N
    O

  23. Sam says:

    How do you print this –
    A
    B B
    C C C
    D D D D
    E E E E E

  24. ashucoderiitian says:

    plzz print this pattern
    A
    AB
    ABC
    ABCD
    ABCDE
    using def function

  25. Praki says:

    What is the code for this
    n = 6
    String = [‘A’, ‘B’, ‘C’]
    OUTPUT:
    A
    AB
    ABC
    ABCA
    ABCAB
    ABCABC

  26. parag bhosale says:

    how to print this

    1
    12A
    123BA
    1234CBA
    for 10 rows like this

  27. Sudheer says:

    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
    How to get this pattern

Leave a Reply

Your email address will not be published. Required fields are marked *