我有以下代码行
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
关于python - 如何在 string.format() 的固定宽度字段中放置 $ 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51737866/