我正在尝试将一个货币添加到我的字典输出中。但是,在我的货币符号和int之间有一个空格。
foodItems = [{"name":"6 Apples", "price": 5.40},
{"name": "Mixed Vegetables Pack 1kg", "price": 9.32},
{"name": "Yakult 5 in a pack", "price": 3.20},
{"name": "Mixed nuts 500g", "price": 16.98},
{"name": "Milk Powder 200g", "price": 9.47},
{"name": "Roasted Chicken Breast 1kg", "price": 8.56}]
#display food items
for i in foodItems:
print("{:<41}${:>6.2f}".format(i["name"],i["price"]))输出:
Food items available for subscription (price/week)
6 Apples $ 5.40
Mixed Vegetables Pack 1kg $ 9.32
Yakult 5 in a pack $ 3.20
Mixed nuts 500g $ 16.98
Milk Powder 200g $ 9.47
Roasted Chicken Breast 1kg $ 8.56如何删除"$“和图形之间的空格,使输出变为
Food items available for subscription (price/week)
6 Apples $5.40
Mixed Vegetables Pack 1kg $9.32
Yakult 5 in a pack $3.20
Mixed nuts 500g $16.98
Milk Powder 200g $9.47
Roasted Chicken Breast 1kg $8.56发布于 2021-05-04 03:20:51
我认为这将需要两个字符串格式化步骤;一个是添加美元符号并将浮点数正确地格式化为货币,另一个是右对齐文本。
print("Food items available for subscription (price/week)")
#display food items
for i in foodItems:
print("{:<41}{:>9}".format(i["name"], "${:.2f}".format(i["price"])))输出
Food items available for subscription (price/week)
6 Apples $5.40
Mixed Vegetables Pack 1kg $9.32
Yakult 5 in a pack $3.20
Mixed nuts 500g $16.98
Milk Powder 200g $9.47
Roasted Chicken Breast 1kg $8.56https://stackoverflow.com/questions/67374377
复制相似问题