1 year ago
#389141
Daalex
Making a table in python with exact spacing
I am already sitting here, several hours, trying to figure out how to properly do this as a complete Python newbie.
I basically want to create a receipt for shopping bakery products.
However, I just cant get the spacing for it done.
My biggest problem currently is, that I cannot do 2 f-string modifiers at once.
It basically is working with :>15s
for example, then I cant use :.2f
in my f-string anymore in order to display 2 decimals.
Artikel Stk.-Preis Anzahl Preis/Euro
------------------------------------------------------------------
Brezen 0.75 54 40.50
Semmeln 0.50 344 172.00
Baguettes 2.10 43 90.30
Dinkel-Vollkornbrot 4.75 2 9.50
Here you can already see the problem in the last column, if i do it without :>(number)s
. It just gets all over the place as soon as it moves from x.xx to xx.xx or even xxx.xx prices.
I tried to do it with print(f"Semmeln {padding:>21s} {semmel_preis:.2f} {str(semmeln):>12s} {padding:>10s} {semmeln_gesamt:.2f}")
, where "padding" is simply ""
, trying to format it.
Is there an easy way to use for exmaple semmel_preis:.2f:.>21s
?
If no, how can formatting like this be done easily so that it lines up, no matter how long the numbers are?
My code is looking like this for the table:
print("\nArtikel \t\tStk.-Preis Anzahl Preis/Euro")
print("------------------------------------------------------------------")
if(brezen>0):
print(f"Brezen {padding:>22s} {breze_preis:.2f} {str(brezen):>12s} {padding:>10s} {brezen_gesamt:.2f}")
if(semmeln>0):
print(f"Semmeln {padding:>21s} {semmel_preis:.2f} {str(semmeln):>12s} {padding:>10s} {semmeln_gesamt:.2f}")
if(baguettes>0):
print(f"Baguettes {padding:>19s} {baguette_preis:.2f} {str(baguettes):>12s} {padding:>9s} {baguettes_gesamt:.2f}")
if(dinkel_vollkorn>0):
print(f"Dinkel-Vollkornbrot {padding:>9s} {dinkel_vollkorn_preis:.2f} {str(dinkel_vollkorn):>12s} {padding:>9s} {dinkel_vollkorn_gesamt:.2f}")
python
formatting
0 Answers
Your Answer