Python 如何在string.format()中的固定宽度字段中放置$符号
我有下面一行代码Python 如何在string.format()中的固定宽度字段中放置$符号,python,Python,我有下面一行代码 print("Income $ {:>8,d} pays Flat tax ${:>9.2f} and Current tax ${:>9.2f}".format(income,tax,curr_tax)) 它给了我输出 Income $ 10,000 pays Flat tax $ 0.00 and Current tax $ 0.00 但是我试图把$符号和数字连在一起,中间没有空格。如何使用string.format() 编辑: 这
print("Income $ {:>8,d} pays Flat tax ${:>9.2f} and Current tax ${:>9.2f}".format(income,tax,curr_tax))
它给了我输出
Income $ 10,000 pays Flat tax $ 0.00 and Current tax $ 0.00
但是我试图把$符号和数字连在一起,中间没有空格。如何使用string.format()
编辑:
这就是我试图实现的输出
Income $50,000 pays Flat tax $780.00 and Current tax $0.00
Income $10,00 pays Flat tax $0.00 and Current tax $0.00
您可以在您的
格式中格式
print("Income {:>8} pays Flat tax {:>9} and Current tax {:>9}".format('${:,d}'.format(income), '${:.2f}'.format(tax), '${:.2f}'.format(curr_tax)))
给
Income $10,000 pays Flat tax $0.00 and Current tax $0.00
Income $50,000 pays Flat tax $780.00 and Current tax $0.00
他有一个可靠的答案
或者,通过将
更改为来使用@student comment。将
更改为如何?可以使用区域设置模块,请参阅
print("Income ${:<8,d} pays Flat tax ${:<9.2f} and Current tax ${:<9.2f}".format(10000,0.00,0.00))
Income $10,000 pays Flat tax $0.00 and Current tax $0.00