1

i am creating a "Stock portfolio" for my school project. The portfolio should show the No,Company name, Cap,Qty,boughtprice and marketprice. I was able to create the 2D list and print it out, however the output is not align. Below is my code:

listofstock=[]
listofstock.append(["No","Company","Cap","Qty","BoughtPrice","MarketPrice"])
listofstock.append([1,"Microsoft","Mega",100,188,207])
listofstock.append([2,"Amazon","Mega",5,1700,3003])
listofstock.append([3,"PayPal","Large",80,100,188])
listofstock.append([4,"Apple","Large",100,60,110])
listofstock.append([5,"Fastly","Mid",30,40,76])
listofstock.append([6,"Square","Mid",30,40,178])

for row in listofstock:
  for col in row:
    print(col," ",end=" ")
  print()

The output is not aligned with my headers at all, how do I make it align?

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

0

You can use tabulate library. Install via pip pip install tabulate.

from tabulate import tabulate

list_of_stock = []
headers = ["No", "Company", "Cap", "Qty", "BoughtPrice", "MarketPrice"]

# append data
list_of_stock.append([1, "Microsoft", "Mega", 100, 188, 207])
list_of_stock.append([2, "Amazon", "Mega", 5, 1700, 3003])
list_of_stock.append([3, "PayPal", "Large", 80, 100, 188])
list_of_stock.append([4, "Apple", "Large", 100, 60, 110])
list_of_stock.append([5, "Fastly", "Mid", 30, 40, 76])
list_of_stock.append([6, "Square", "Mid", 30, 40, 178])

# show data
print(tabulate(list_of_stock, headers=headers))
  No  Company    Cap      Qty    BoughtPrice    MarketPrice
----  ---------  -----  -----  -------------  -------------
   1  Microsoft  Mega     100            188            207
   2  Amazon     Mega       5           1700           3003
   3  PayPal     Large     80            100            188
   4  Apple      Large    100             60            110
   5  Fastly     Mid       30             40             76
   6  Square     Mid       30             40            178
t4kq
  • 754
  • 1
  • 5
  • 11
0

Using print and string formatter syntax with right alignment would suffice:

print("{:>11}".format(col), end=" ")

Like so:

for row in listofstock:
  for col in row:
    print("{:>11}".format(col), end=" ")
  print()
rbento
  • 9,919
  • 3
  • 61
  • 61
0

You can do basic match to calculate the padding

listofstock=[]

listofstock.append(["No","Company","Cap","Qty","BoughtPrice","MarketPrice"])

listofstock.append([1,"Microsoft","Mega",100,188,207])

listofstock.append([2,"Amazon","Mega",5,1700,3003])

listofstock.append([3,"PayPal","Large",80,100,188])

listofstock.append([4,"Apple","Large",100,60,110])

listofstock.append([5,"Fastly","Mid",30,40,76])

listofstock.append([6,"Square","Mid",30,40,178])

# store max length for each column
column_max_lengths = [max(len(str(j[i]))+5 for j in listofstock) for i in range(len(listofstock[0]))]
# +5 for extra padding

for row in listofstock: 
    for index, col in enumerate(row): 
        print(str(col).ljust(column_max_lengths[index]),end="")
    print()
No     Company       Cap       Qty     BoughtPrice     MarketPrice     
1      Microsoft     Mega      100     188             207             
2      Amazon        Mega      5       1700            3003            
3      PayPal        Large     80      100             188             
4      Apple         Large     100     60              110             
5      Fastly        Mid       30      40              76              
6      Square        Mid       30      40              178
Epsi95
  • 8,832
  • 1
  • 16
  • 34