Print multiline strings side-by-side

前端 未结 4 595
长情又很酷
长情又很酷 2020-12-20 06:42

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

dice_art = [\"\"\"
 -------
|       |
|   N   |
|       |
 ------- \"\"\",\"\"         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-20 07:23

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

提交回复
热议问题