Print multiline strings side-by-side

前端 未结 4 591
长情又很酷
长情又很酷 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

    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.

提交回复
热议问题