python - 用 $ 打印它,如使用 python 格式所示
问题描述
我需要将 $ 添加到价格部分,如此处所示 1
如何使用 f-string 做到这一点
for name, shares, price, change in report:
print(f'{name:>10s} {shares:>10d} {price:>10.2f} {change:>10.2f}')
解决方案
report = [
['AA', 100, 9.22, -22.98],
['IBM', 50, 106.28, 15.18]
]
for name, shares, price, change in report:
price = f'${price:.2f}'
print(f'{name:>10} {shares:>10d} {price:>10} {change:>10.2f}')
印刷:
AA 100 $9.22 -22.98
IBM 50 $106.28 15.18
推荐阅读
- c# - 在关键字'as'之后检查实例是基类还是派生类?
- multithreading - 多客户端 TCP 套接字服务器(服务器在连接时发送查询然后等待客户端响应 + 轮询客户端)
- azure-active-directory - 使用 powershell 为应用配置 Azure AD 访问令牌生命周期策略不起作用
- windows - 任务栏中的 Inno Setup 窗口预览
- python-3.x - 如何用python播放声音而没有while循环
- yarnpkg - 当我尝试升级某些依赖项时,为什么 Yarn 会抛出“不变违规:预期的工作区包存在”?
- c++ - 当 sockaddr_in 作为普通变量传递给服务器对象时,客户端套接字无法连接到服务器套接字
- masstransit - ConfigurationHostSettings 是内部的
- python - 在 Python 中,在文件中搜索行号只工作一次,然后只返回 0
- excel - VBA检测列格式是否为日期