-4
# 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

2 Answers2

0
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]: 

Commentary


rock, etc are single strings containing a few newline characters, and we want to print, on the same line, a line from rock, a line from paper and one from scissors, so the first thing that we need to do is to split each string to obtain a list of list of strings

list_of_lists_of_strings = [thing.split('\n') for thing in (rock, paper, scissors)]
# [['rock', '    _______', ...], ['paper', '    _______', ...], [...]]

but we really need

[['rock', 'paper', 'scissors'],
 ['    _______', '    _______', '    _______'],
 [..., ..., ...]
 ...,
]

that is, the transpose of the list of lists of strings, but this is a well known idiom in Python (it's not really a list, but…)

transposed_list = zip(*list_of_lists_of_strings)

at this point we can print each triple of elements in the transposed_list using an appropriate format (%-25s outputs a string 25 characters long, blank filled on the right).

Making this a function is left as an exercise…


Sub—commentary


IMO, as far as possible a function shouldn't provide side effects, like printing.

gboffi
  • 22,939
  • 8
  • 54
  • 85
0

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                
     _______            _______              _______            _______         
---'    ____)____   ---'   ____)____    ---'    ____)____   ---'   ____)        
           ______)            ______)              ______)        (_____)       
          _______)         __________)            _______)        (_____)       
         _______)         (____)                 _______)         (____)        
         _______)   ---.__(___)                  _______)   ---.__(___)         
         _______)                                _______)                       
---.__________)                         ---.__________)                         
mcsoini
  • 6,280
  • 2
  • 15
  • 38