Home > Software design >  printing 2 stars using strings side by side
printing 2 stars using strings side by side

Time:01-31

Without using the function call, how can I modify my string to have the stars come out as side by side?

This is what I did:

print("          *\n       *     *\n     *         *\n   *             *\n  * * *        * * *\n"
    "      *        *\n      *        *\n      **********" , end = "\n          *\n       *     *\n     *         *\n   *             *\n  * * *        * * *\n"
    "      *        *\n      *        *\n      **********" )

but this results in the stars coming out as top and bottom. I want it to print side by side.

CodePudding user response:

#pass one arrow in a variable y
y = '''          *\n       *     *\n     *         *\n   *             *\n  * * *        * * *\n      *        *\n      *        *\n      **********''' 
#split on the linebreak
spl = y.split('\n')  
#get width of the arrow
max_len = max([len(x) for x in spl])
#compute space for new arrow points as max width - position of last element 
#of first arrow and repeat after space
new = [x   (max_len - len(x))*' '  x for x in spl]
#join back on linebreaks
new = '\n'.join(new)
print(new)

          *                   *
       *     *             *     *
     *         *         *         *
   *             *     *             *
  * * *        * * *  * * *        * * *
      *        *          *        *
      *        *          *        *
      **********          **********

CodePudding user response:

You could use a triple quoted multi-line string to create an "arrow template" then utilize .splitlines() and zip() to print them side by side(with optional spacing between).

UP_ARROW = """
        *         
     *     *      
   *         *    
 *             *  
* * *        * * *
    *        *    
    *        *    
    **********    
""".strip("\n").splitlines()

for arrows in zip(UP_ARROW, [" "] * len(UP_ARROW), UP_ARROW):
    print(*arrows)

Output:

        *                    *         
     *     *              *     *      
   *         *          *         *    
 *             *      *             *  
* * *        * * *   * * *        * * *
    *        *           *        *    
    *        *           *        *    
    **********           **********    
  • Related