Enya
Enya

Reputation: 33

Printing multiline strings, horizontally stacked and aligned

I have this list:

boxes = [
'''
 _ _ _ _ _
|         |
|         |
|   1st   |
|   box   |
|         |
|_ _ _ _ _|
''',
'''
 _ _ _ _ _
|         |
|         |
|   2nd   |
|   box   |
|         |
|_ _ _ _ _|
''',
'''
 _ _ _ _ _
|         |
|         |
|   3rd   |
|   box   |
|         |
|_ _ _ _ _|
''',

etc...]

And this is the way I want to display the elements of the list:

 _ _ _ _ _   _ _ _ _ _   _ _ _ _ _
|         | |         | |         |
|         | |         | |         |
|   1st   | |   2nd   | |   3rd   |
|   box   | |   box   | |   box   |
|         | |         | |         |
|_ _ _ _ _| |_ _ _ _ _| |_ _ _ _ _|
   

Is there a way to do this without hardcoding the inline format? (The content of each box changes)

The only way I could think about is:

for x in boxes:
    print(x, end=' ')

so I've tried doing this but it doesn't work, any ideas? Thank you!

Upvotes: 3

Views: 181

Answers (3)

tripleee
tripleee

Reputation: 189908

You can split each box into lines, and assemble a combined line to print with the corresponding line from each box formatted adjacent to each other at a fixed width.

lines = [box.splitlines() for box in boxes]
for idx in range(len(lines[0])):
    print('%-20.20s %-20.20s %-20.20s' % (lines[0][idx], lines[1][idx], lines[2][idx]))

This could obviously be generalized in a number of ways, but should at least get you started. It simply assumes that all the array members contain the same number of lines, and hardcodes the formatting width and the number of array members.

Demo: https://ideone.com/VOfEt9

Some of the hardcoding could be lifted, but this is probably a bit notationally dense for a beginner question:

    print(' '.join(['{:20}'] * len(lines)).format(*list(elt[idx] for elt in lines)))

Upvotes: 2

wim
wim

Reputation: 363476

Tabulate supports multiline cells.

>>> from tabulate import tabulate
>>> print(tabulate([boxes], tablefmt="plain"))

 _ _ _ _ _    _ _ _ _ _    _ _ _ _ _
|         |  |         |  |         |
|         |  |         |  |         |
|   1st   |  |   2nd   |  |   3rd   |
|   box   |  |   box   |  |   box   |
|         |  |         |  |         |
|_ _ _ _ _|  |_ _ _ _ _|  |_ _ _ _ _|

Unlike the other answers, it will adapt to different shaped cells nicely:

>>> boxes[1] = """
...  _ _ _
... |     |
... | 2nd |
... | box |
... |_ _ _|
... """
>>> print(tabulate.tabulate([boxes], tablefmt="plain"))

 _ _ _ _ _    _ _ _    _ _ _ _ _
|         |  |     |  |         |
|         |  | 2nd |  |         |
|   1st   |  | box |  |   3rd   |
|   box   |  |_ _ _|  |   box   |
|         |           |         |
|_ _ _ _ _|           |_ _ _ _ _|

Upvotes: 1

Matiiss
Matiiss

Reputation: 6176

I see I am late to this but here is my solution:

boxes = [
'''
 _ _ _ _ _
|         |
|         |
|   1st   |
|   box   |
|         |
|_ _ _ _ _|
''',
'''
 _ _ _ _ _
|         |
|         |
|   2nd   |
|   box   |
|         |
|_ _ _ _ _|
''',
'''
 _ _ _ _ _
|         |
|         |
|   3rd   |
|   box   |
|         |
|_ _ _ _ _|
''',
    
]

split_boxes = []

for box in boxes:
    box = box.split('\n')
    split_boxes.append(box)

for i in range(len(split_boxes[0])):
    final = ''
    for split_box in split_boxes:
        if i == 1:
            final += split_box[i] + '  '
        else:
            final += split_box[i] + ' '
    print(final)

Upvotes: 1

Related Questions