Python Program to Print A-BAB-CBABC Pattern Up To n Lines


This python program prints A-BAB-CBABC-DCBABCD patterns up to n lines given by user.


In this program %c is used for displaying corresponding ASCII character of a number. For example to display ASCII character corresponding to 65; print("%c" %(65)) which gives A as output.

Python Source Code: A BAB CBABC Pattern


# Pattern A-BAB-CBABC ...

# Reading number of rows
row = int(input('Enter how many lines? '))

a = 64
# Generating pattern
for i in range(1,row+1):
    
    # for space
    for j in range(1, row+1-i):
        print(' ', end='')
    
    # for decreasing pattern
    for j in range(i,0,-1):
        print('%c' % (a+j), end='')
    
    # for increasing pattern 
    for j in range(2,i+1):
        print('%c' % (a+j), end='')
    
    # Moving to next line
    print()

Output

Enter how many lines? 6

     A
    BAB
   CBABC
  DCBABCD
 EDCBABCDE
FEDCBABCDEF