【问题标题】:I dont understand what those values in the curly bracket represent我不明白大括号中的那些值代表什么
【发布时间】:2019-12-11 04:16:09
【问题描述】:

我正在使用在线教程,作者使用 output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds) 但我不明白 {0:02}{1:02} 中的值是什么。

【问题讨论】:

标签: python python-3.x string python-2.7 formatting


【解决方案1】:
output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
  • {...} 称为替换字段。它包含说明,说明应该去那里以及应该如何看待。
  • 0:02 - 冒号左侧的值是字段名称。它指定替换字段的内容 - 在本例中,它是一个索引,0.format() 的第一个参数将替换此替换字段。
  • 0:02 - 冒号右侧的值指定 它的外观 - 格式规范。在这种情况下,它指定 2 个字符的宽度,并且如果替换只有一个字符(从左侧)填充为 '0'

Format String Syntax
Format Specification Mini-Language

【讨论】:

  • 非常感谢。我现在完全明白了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2022-08-08
  • 1970-01-01
  • 2018-09-12
相关资源
最近更新 更多