Search code examples
pythonprintingformatting

Python: How to print a 5x5 grid on the same line as another


I'm trying to build a function that prints a given string in large text through command line. Each letter is a 5x5 grid of *'s and spaces. How would I print the next 5x5 character on the same line as the previous to print out the given string?

Code:

    options = {
    'a': '  *  \n * * \n*****\n*   *\n*   *', 
    'b': '**** \n*   *\n*****\n*   *\n**** ',
    'c': ' ****\n*    \n*    \n*    \n ****',
    'd': '**** \n*   *\n*   *\n*   *\n**** ',
    'e': '*****\n*    \n*****\n*    \n*****',
    'f': '*****\n*    \n*****\n*    \n*    ',
    'g': '*****\n*    \n* ***\n*   *\n*****',
    'h': '*   *\n*   *\n*****\n*   *\n*   *',
    'i': '*****\n  *  \n  *  \n  *  \n*****',
    'j': '***  \n  *  \n  *  \n  *  \n***  ',
    'k': '*   *\n* *  \n*    \n* *  \n*   *',
    'l': '*    \n*    \n*    \n*    \n*****',
    'm': '*   *\n** **\n* * *\n*   *\n*   *',
    'n': '*   *\n**  *\n* * *\n*  **\n*   *',
    'o': ' *** \n*   *\n*   *\n*   *\n *** ',
    'p': '**** \n*   *\n**** \n*    \n*    ',
    'q': ' *** \n*   *\n* * *\n*  * \n ** *',
    'r': '**** \n*   *\n**** \n* *  \n*  **',
    's': ' ****\n*    \n *** \n    *\n**** ',
    't': '*****\n  *  \n  *  \n  *  \n  *  ',
    'u': '*   *\n*   *\n*   *\n*   *\n *** ',
    'v': '*   *\n*   *\n * * \n * * \n  *  ',
    'w': '*   *\n*   *\n* * *\n* * *\n * * ',
    'x': '*   *\n * * \n  *  \n * * \n*   *',
    'y': '*   *\n * * \n  *  \n  *  \n  *  ',
    'z': '*****\n   * \n  *  \n *   \n*****',
    ',': '     \n     \n   **\n   **\n  *  ',
    ':': '     \n  *  \n     \n  *  \n     '

    }

    def Print_Big(inputString):
        lst = list(inputString)
        for i in lst:
            print(options[i], end = "")

    while True:
        userIn = input('Please enter a letter to big-ify: ').lower()
if userIn == "exit":
    break
elif userIn != "" and len(userIn) >= 1:
    Print_Big(userIn)
else:
    print('Please enter a valid string')

Solution

  • You can't print different letters side by side because you have \n (new line character) in every letters you made. So by default every next element will be printed in the next line. Now to overcome this issue,I made some changes in your code. Make a dictionary of lists in your code for each letters as follows.

    options = {
            'a':['  *  ',' * * ','*****','*   *','*   *'],
            'b':['**** ','*   *','*****','*   *','**** '],
            'c':[' ****','*    ','*    ','*    ',' ****'],
             ........
            }
    

    Why dictionary of lists? because I can access each lines of a letter one by one now. I have given the code sample here. its working fine for 3 characters a,b,c as I added only 3 letters into dict for the purpose of demonstration.

    options = {         
            'a':['  *  ',' * * ','*****','*   *','*   *'],
            'b':['**** ','*   *','*****','*   *','**** '],
            'c':[' ****','*    ','*    ','*    ',' ****']
            }   # I added only 3 letters, so It will work for only(a,b,c)
    
    def Print_Big(newList):
        for i in range(len(options['a'])):  
            for j in range(len(newList)):       
                print(options[newList[j]][i]+"   ",end = " ")
            print()
    

    output:

    Please enter a letter to big-ify: abc
        *      ****      ****
       * *     *   *    *
      *****    *****    *
      *   *    *   *    *
      *   *    ****      ****