Ozixic
Ozixic

Reputation: 141

Print multiline strings side-by-side

I want to print the items from a list on the same line. The code I have tried:

dice_art = ["""
 -------
|       |
|   N   |
|       |
 ------- ""","""
 -------
|       |
|   1   |
|       |
 ------- """] etc...

player = [0, 1, 2]
for i in player:
    print(dice_art[i], end='')

output =

ASCII0
ASCII1
ASCII2

I want output to =

ASCII0 ASCII1 ASCII2

This code still prints the ASCII art representation of my die on a new line. I would like to print it on the same line to save space and show each player's roll on one screen.

Upvotes: 1

Views: 3894

Answers (4)

Arya McCarthy
Arya McCarthy

Reputation: 8829

The fact that your boxes are multiline changes everything.

Your intended output, as I understand it, is this:

 -------  -------
|       ||       |
|   N   ||   1   | ...and so on...
|       ||       |
 -------  ------- 

You can do this like so:

art_split = [art.split("\n") for art in dice_art]
zipped = zip(*art_split)

for elems in zipped:
    print("".join(elems))
#  -------  -------
# |       ||       |
# |   N   ||   1   |
# |       ||       |
#  -------  ------- 

N.B. You need to guarantee that each line is the same length in your output. If the lines of hyphens are shorter than the other, your alignment will be off.

In the future, if you provide the intended output, you can get much better responses.

Upvotes: 5

vaultah
vaultah

Reputation: 46533

Since the elements of dice_art are multiline strings, this is going to be harder than that.

First, remove newlines from the beginning of each string and make sure all lines in ASCII art have the same length.

Then try the following

player = [0, 1, 2]
lines = [dice_art[i].splitlines() for i in player]
for l in zip(*lines):
    print(*l, sep='')

If you apply the described changes to your ASCII art, the code will print

 -------  -------  ------- 
|       ||       ||       |
|   N   ||   1   ||   2   |
|       ||       ||       |
 -------  -------  ------- 

Upvotes: 4

yifan
yifan

Reputation: 72

A join command should do it.

dice_art = ['ASCII0', 'ASCII1', 'ASCII2']
print(" ".join(dice_art))

The output would be:

ASCII0 ASCII1 ASCII2

Upvotes: 0

David Lunt
David Lunt

Reputation: 60

Change print(dice_art[i], end='') to:

  • print(dice_art[i], end=' '), (Notice the space inbetween the two 's and the , after your previous code)

If you want to print the data dynamically, use the following syntax:

  • print(dice_art[i], sep=' ', end='', flush=True),

Upvotes: -1

Related Questions