Converting an input string into a beautiful star pattern

  • Before going to the program, I wiil show you the output first
    Enter your Name
    shashank
     **** *   *   *    **** *   *   *   *   * *   * 
    *     *   *  * *  *     *   *  * *  **  * *  *  
     ***  ***** *   *  ***  ***** *   * * * * ***   
        * *   * *****     * *   * ***** *  ** *  *  
    ****  *   * *   * ****  *   * *   * *   * *   * 
  • If we write a code for every letters in a single code it might may become lengthy. even though we write in seperate functions 26 functions need to write.

  • I write the single letter code in a seperate file and 26 file contains a seperate code of that letter apart from that another file has been taken to run main program which will get together all these files into one.

  • Only disadvantage of this approach is we need 27 files to execute but we learn a new thing i.e., how can we call a method which is present at different file.

  • For this project we need to take a seperate folder, Inside that single folder only we need to create a 26 files for letters and 1 main file.

  • I have imported a file using import as shown below

    from A import *
    # A is a python file of format '.py' and * means everything in file A has been imported
  • Note that I have written every code in a seperate file
  • code for pattern A. This code in file A.py

    def A():
        print_A = [[" " for i in range(5)] for j in range(5)]     #empty two dimensional list of range 5x5
        for row in range(5):
            for col in range(5):
                if((row >= 2 and col == 0)or(row==1 and col==1)or(row==0 and col==2)or(row >= 2 and col == 4)or (row==3)or(row==1 and col==1) or (row==1 and col==3)or(row==0 and (col==2))):
                      print_A[row][col]= "*"                      #storing a pattern of A 
        return print_A                                            #returning pattern
  • code for pattern B. This code in file B.py

    def B():
        print_B = [[" " for i in range(5)] for j in range(5)]
        for row in range(5):
            for col in range(5):
                if(col == 0 or (row == 0 and col!=4) or (row == 1  and col ==4)or(row == 4 and col != 4)or(row == 3 and col == 4)or(row == 2 and col != 4)):
                    print_B[row][col]="*"
        return print_B    
    
  • Like above I have wriiten 26 code A-Z in seperate file.You can make your own code to look alphabets more attractive.
  • The main program code. This code is also in the same folder and I named this as main.py
    #importing files
    from A import *
    from B import *
    from C import *
    from D import *
    from E import *
    from F import *
    from G import *
    from H import *
    from I import *
    from J import *
    from K import *
    from L import *
    from M import *
    from N import *
    from O import *
    from P import *
    from Q import *
    from R import *
    from S import *
    from T import *
    from U import *
    from V import *
    from W import *
    from X import *
    from Y import *
    from Z import *
    
    #function to find the input character and to call the respective code for that input text
    def pattern():
        for i in range(len(name)):
            if(name[i] == 'A'):
                list1 = A()                           #calling a function A() which is present at file A.py
                list.append(list1)
            elif(name[i] == 'B'):
                list1 = B()
                list.append(list1)
            elif(name[i] == 'C'):
                list1 = C()
                list.append(list1)
            elif(name[i] == 'D'):
                list1 = D()
                list.append(list1)
            elif(name[i] == 'E'):
                list1 = E()
                list.append(list1)
            elif(name[i] == 'F'):
                list1 = F()
                list.append(list1)
            elif(name[i] == 'G'):
                list1 = G()
                list.append(list1)
            elif(name[i] == 'H'):
                list1 = H()
                list.append(list1)
            elif(name[i] == 'I'):
                list1 = I()
                list.append(list1)
            elif(name[i] == 'J'):
                list1 = J()
                list.append(list1)
            elif(name[i] == 'K'):
                list1 = K()
                list.append(list1)
            elif(name[i] == 'L'):
                list1 = L()
                list.append(list1)
            elif(name[i] == 'M'):
                list1 = M()
                list.append(list1)
            elif(name[i] == 'N'):
                list1 = N()
                list.append(list1)
            elif(name[i] == 'O'):
                list1 = O()
                list.append(list1)
            elif(name[i] == 'P'):
                list1 = P()
                list.append(list1)
            elif(name[i] == 'Q'):
                list1 = Q()
                list.append(list1)
            elif(name[i] == 'R'):
                list1 = R()
                list.append(list1)
            elif(name[i] == 'S'):
                list1 = S()
                list.append(list1)
            elif(name[i] == 'T'):
                list1 = T()
                list.append(list1)
            elif(name[i] == 'U'):
                list1 = U()
                list.append(list1)
            elif(name[i] == 'V'):
                list1 = V()
                list.append(list1)
            elif(name[i] == 'W'):
                list1 = W()
                list.append(list1)
            elif(name[i] == 'Y'):
                list1 = Y()
                list.append(list1)
            elif(name[i] == 'Y'):
                list1 = Y()
                list.append(list1)
            elif(name[i] == 'Z'):
                list1 = Z()
                list.append(list1)
        return list
            
    name = input("Enter your Name\n")
    name = name.upper()                 #it will make all the code to uppercase
    list = []                           #Empty list which will give output in pattern
    list1 = pattern()                   #this list stores the pattern
    
    for i in range(5):                               #row
        for j in range(len(list1)):                  #coloumn
            for k in range(5):                       #coloumn to start next letter
                print(list[j][i][k],end="")
            print(end=" ")                           #space betweeen every letter
        print()
  • We can alter the code in many ways.

How it works?

  • Converting a string to pattern is easy but oredering side by side is quite logic.
  • So i have taken a list which will make us to print side by side.
  • append() function will going to append the paatern upto the length of the string.
  • If we execute, while it is executing we can notice that it will not printing character by character, It is going to print line by line.

#qurantine_time_pass

Posted on by