Oct-31-2021, 10:38 AM
Add first data i structure eg list, dictionary.
Then can use f-string or eg python-tabulate.
Don't use old string formatting anymore
Example.
Then can use f-string or eg python-tabulate.
Don't use old string formatting anymore
%ds %
.Example.
from tabulate import tabulate item = 'Milk' price = 100 quantity = 5 total = 500 item_lst = [] item_lst.append(item) item_lst.append(price) item_lst.append(quantity) item_lst.append(total) lst = [] header = ['Item', 'Price', 'Quantity', 'Total'] lst.append(header) lst.append(item_lst) # f-string for inner in lst: print(''.join((f"{word:<10}" for word in inner))) print('-' * 35) # python-tabulate print(tabulate(lst)) print(tabulate(lst, tablefmt="fancy_grid")) print(tabulate(lst, tablefmt="github"))
Output:Item Price Quantity Total
Milk 100 5 500
-----------------------------------
---- ----- -------- -----
Item Price Quantity Total
Milk 100 5 500
---- ----- -------- -----
╒══════╤═══════╤══════════╤═══════╕
│ Item │ Price │ Quantity │ Total │
├──────┼───────┼──────────┼───────┤
│ Milk │ 100 │ 5 │ 500 │
╘══════╧═══════╧══════════╧═══════╛
|------|-------|----------|-------|
| Item | Price | Quantity | Total |
| Milk | 100 | 5 | 500 |
Also eg Pandas easier to manipulate data eg NoteBook.