Home > Blockchain >  The best and shortest way to print multiline strings (ascii art) on the same line, even when the hei
The best and shortest way to print multiline strings (ascii art) on the same line, even when the hei

Time:06-07

# Rock
rock = ("""Rock
    _______
---'   ____)
      (_____)
      (_____)   
      (____)
---.__(___)
""")

# Paper
paper = ("""Paper
     _______
---'    ____)____
           ______)
          _______)  
         _______)
---.__________)
""")

# Scissors
scissors = ("""Scissors
    _______
---'   ____)____
          ______)
       __________)  
      (____)
---.__(___)
""")

how can I print these multiline strings at the same line?

I'm looking for the simplest and shortest way I tried few tricks that I saw, but no luck so far.

thanks

CodePudding user response:

In [19]: for rps in zip(*(thing.split('\n') for thing in (rock, paper, scissors))):
    ...:     print(("%-25s"*3)%(rps))
    ...: 
Rock                     Paper                    Scissors                 
    _______                   _______                 _______              
---'   ____)             ---'    ____)____        ---'   ____)____         
      (_____)                       ______)                 ______)        
      (_____)                      _______)              __________)       
      (____)                      _______)              (____)             
---.__(___)              ---.__________)          ---.__(___)              
                                                                           

In [20]: 

More flexibility?

In [24]: def pol(*list_of_things):
    ...:     return '\n'.join(("%-25s"*len(list_of_things))%(rps) for rps in zip(*(thing.split('\n') for thing in list_of_things)))

In [25]: print(pol(scissors, scissors, rock))
Scissors                 Scissors                 Rock                     
    _______                  _______                  _______              
---'   ____)____         ---'   ____)____         ---'   ____)             
          ______)                  ______)              (_____)            
       __________)              __________)             (_____)            
      (____)                   (____)                   (____)             
---.__(___)              ---.__(___)              ---.__(___)              
                                                                           

In [26]: 

CodePudding user response:

Arbitrary number of images on one line (paper got an extra finger to demonstrate that it still works for different image sizes):

import itertools

def print_on_line(*images):
    lines = list(map(str.splitlines, images))
    max_length = max(map(len, itertools.chain.from_iterable(lines)))
    lines_padded = [[line.ljust(max_length, " ") for line in image]
                    for image in lines]
    lines_combined = itertools.zip_longest(*lines_padded, 
                                           fillvalue=" " * max_length)
    print("\n".join(map("".join, lines_combined)))


print_on_line(paper, scissors, paper, rock)


# out:
Paper               Scissors            Paper               Rock                
     _______            _______              _______            _______         
---'    ____)____   ---'   ____)____    ---'    ____)____   ---'   ____)        
           ______)            ______)              ______)        (_____)       
          _______)         __________)            _______)        (_____)       
         _______)         (____)                 _______)         (____)        
         _______)   ---.__(___)                  _______)   ---.__(___)         
         _______)                                _______)                       
---.__________)                         ---.__________)                         

CodePudding user response:

If your question is how to print in one line (same line) these multiline strings you can use the newline and tab characters ('\n' and '\t'), for example:

print("Rock\n    _______\n---'   ____)\n      (_____)\n      (_____)\n      (____)\n---.__(___)\n")
  • Related