Python using Idle How Do I align my output to the right without entering a bunch
ID: 3745305 • Letter: P
Question
Python using Idle
How Do I align my output to the right without entering a bunch of spaces? in the print command
#input
Num_Shares=float(input("Enter number of shares: "))
Stock_Purchase_Price=float(input("Enter purchase price: "))
Stock_Sell_Price=float(input("Enter Selling price: "))
Commission=float(input("Enter Commission: "))
#Calculations
paid=Num_Shares*Stock_Purchase_Price
Commission_paid_purchase=paid*Commission
Sold = Num_Shares * Stock_Sell_Price
Commission_paid_Sell=Sold*Commission
Profit_Loss=(Sold-Commission_paid_Sell)-(paid+Commission_paid_purchase)
#output
print(" Stock Name: Kaplack,. Inc.")
print(" Amount paid for the stock: $",format(paid,',.2f'))
print("Commission paid on the purchase: $",format(Commission_paid_purchase,',.2f'))
print("Amount the stock sold for: $ ",format(Sold,',.2f'))
print("Commission paid on the sale: $",format(Commission_paid_Sell,',.2f'))
print("Profit (or loss if negative): $ ",format(Profit_Loss,',.2f'))
Explanation / Answer
you have to use > symbol which aligns the output to the right.
Eg: >15.2f which aligns the output to the right with width of 15 rounded off to 2 decimal places
#input
Num_Shares=float(input("Enter number of shares: "))
Stock_Purchase_Price=float(input("Enter purchase price: "))
Stock_Sell_Price=float(input("Enter Selling price: "))
Commission=float(input("Enter Commission: "))
#Calculations
paid=Num_Shares*Stock_Purchase_Price
Commission_paid_purchase=paid*Commission
Sold = Num_Shares * Stock_Sell_Price
Commission_paid_Sell=Sold*Commission
Profit_Loss=(Sold-Commission_paid_Sell)-(paid+Commission_paid_purchase)
#output
print(" Stock Name: Kaplack,. Inc.")
print(" Amount paid for the stock: $",format(paid,'>16.2f'))
print("Commission paid on the purchase: $",format(Commission_paid_purchase,'>11.2f'))
print("Amount the stock sold for: $ ",format(Sold,'>15.2f'))
print("Commission paid on the sale: $",format(Commission_paid_Sell,'>15.2f'))
print("Profit (or loss if negative): $ ",format(Profit_Loss,'>13.2f'))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.