python - 如何在 string.format() 的固定宽度字段中放置 $ 符号

标签 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()

来做到这一点

编辑:

这是我想要实现的输出

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/

相关文章:

python - Python 初学者

python - 如何为项目在 PYPI 上设置 "front page"文档?

python - 将文件从 URL 传输到 Cloud Storage

python - 为指向 ctypes 结构的指针释放内存时出错?

python - 计算字典中的 bool 值

python - 嵌套列表和 count()

python - Ruby 中是否有等同于 yaml.safe_load 的东西?

java - Java/Python 的球形几何库

python - Pandas to_csv 不输出文件

python - OpenCV Python 不支持的数组类型错误