L
L
lil.fxrrx2021-10-04 16:58:19
Python
lil.fxrrx, 2021-10-04 16:58:19

Python how to pack text into a square shape?

Hello everyone, how can I pack the text of the test type into a square shape:

+---------+
|    test    |
+---------+

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-10-04
@mxrdxfxrrx

pip install tabulate

from tabulate import tabulate

table = [
    ["Sun",696000,1989100000],
    ["Earth",6371,5973.6],
    ["Moon",1737,73.5],
    ["Mars",3390,641.85]
]
headers = ['planet', 'R(km)', 'mass']

print(tabulate(table, headers, tablefmt="grid"))

+----------+---------+---------------+
| planet   |   R(km) |          mass |
+==========+=========+===============+
| Sun      |  696000 |    1.9891e+09 |
+----------+---------+---------------+
| Earth    |    6371 | 5973.6        |
+----------+---------+---------------+
| Moon     |    1737 |   73.5        |
+----------+---------+---------------+
| Mars     |    3390 |  641.85       |
+----------+---------+---------------+

Yes, and it's easy to implement yourself:
num = 20
print('-'*num, '|'+'test'.center(num-2)+'|', '-'*num, sep='\n')

--------------------
|       test       |
--------------------

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question