JEE JEE - 3 months ago 10
Python Question

How to make sure output is same length?

I want to know is there a way in python to make sure that the output is the same length , by adding white space or something like that.

78364721 apple 3 3 9
35619833 orange 4 2 8
46389121 chicken 1 10 10
total price of order £ 27


I want :

78364721 apple 3 3 9
35619833 orange 4 2 8
46389121 chicken 1 10 10
total price of order £ 27


Current Code :

All_list=[items,product_list,items2,indprice_list,newprice_list]
for a in zip(*All_list):
print(*a)

print("total price of order","£",total_price)


46389121 chicken 2 10 20
total price of order £ 40
46389121 chicken 2 10 20
total price of order £ 40

BPL BPL
Answer

Here's a simple example using format string syntax:

# -*- coding: utf-8 -*-
dataframe = [
    [78364721, "apple", 3, 3, 9],
    [35619833, "orange", 4, 2, 8],
    [46389121, "chicken", 1, 10, 10]
]

for row in dataframe:
    print('{:8d}  {:10s}{:3d}{:3d}{:3d}'.format(
        row[0], row[1], row[2], row[3], row[4]))

print('total price of order \u00a3 {0}'.format(
    sum([row[4] for row in dataframe])))

If you want anything fancier than this I'd recommend you start using pandas

Comments