Python 如何将一个多行字符串(包含`\n`)居中打印在控制台的中央?

Python how to center a multiline string (containing `\n`) for printing in the center of the console?

我想在打印 terminal/console.

之前将多行字符串(包含换行符 (\n))居中

columns = shutil.get_terminal_size().columns 给我输出控制台的宽度,print(""" test string """.center(columns)) 将字符串打印到中心。

下面的代码打印了一个带有点坐标的框的表示:

import shutil
X1,Y1,X2,Y2 = 90, 120, 162, 161
def print_box():
    def foo(a,b):
        if a<b: return ((b-a)//2)+a
        else  : return ((a-b)//2)+b

    center_line = foo(X1,X2),foo(Y1,Y2)
    tc  = foo(X1,center_line[0]),foo(Y1,center_line[1])
    bc = foo(X2,center_line[0]),foo(Y2,center_line[1])
    
    string = f"""
           Main box 

     {(X1,Y1)}
     (x1,y1) ------------
     |                  |
     |     Top box      |  
     |    {   tc   }    |
     |                  |
     |------------------| {center_line}
     |                  |
     |    Bottom box    |  
     |    {   bc   }    |
     |                  |
     -------------(x2,y2)  
                {(X2,Y2)}
             """
    columns = shutil.get_terminal_size().columns
    print(f"""columns {columns}""".center(columns)) # MULTI LINE STRING WITH NO NEW LINES
    print(f"""columns\n {columns}""".center(columns)) # MULTI LINE STRING WITH NEW LINES
    
    print(string.center(columns)) # MULTI LINE STRING WITH NEW LINES
print_box()

运行 它输出:

                                               columns 104                                               
                                              columns
 104

           Main box

     (90, 120)
     (x1,y1) ------------
     |                  |
     |     Top box      |
     |    (108, 130)    |
     |                  |
     |------------------| (126, 140)
     |                  |
     |    Bottom box    |
     |    (144, 150)    |
     |                  |
     -------------(x2,y2)
                (162, 161)


我要它输出:

                                                columns 104                                               
                                                columns 
                                                104

                                                       Main box

                                                 (90, 120)
                                                 (x1,y1) ------------
                                                 |                  |
                                                 |     Top box      |
                                                 |    (108, 130)    |
                                                 |                  |
                                                 |------------------| (126, 140)
                                                 |                  |
                                                 |    Bottom box    |
                                                 |    (144, 150)    |
                                                 |                  |
                                                 -------------(x2,y2)
                                                            (162, 161)


不幸的是,str.center 不能像您希望的那样处理多行字符串。

你可以试试

> "\n".join(line.center(columns)  for line in string.split("\n"))

string 分成每一行,然后在每一行上执行单独的 str.center