0

I was working with an online tutorial and the author used output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds) but I don't understand what the values in {0:02} and {1:02} are for.

trotta
  • 1,232
  • 1
  • 16
  • 23
  • 1
    you've tagged [string] and [formatting], there's a hint – Chris_Rands Aug 02 '19 at 14:08
  • 1
    https://docs.python.org/3/library/stdtypes.html#str.format ... https://docs.python.org/3/library/string.html#format-string-syntax ... https://docs.python.org/3/library/string.html#format-specification-mini-language – wwii Aug 02 '19 at 14:09
  • 1
    Related [String formatting: % vs. .format](https://stackoverflow.com/questions/5082452/string-formatting-vs-format) – wwii Aug 02 '19 at 14:28

1 Answers1

0
output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
  • {...} is called the replacement field. It contains instructions on what is supposed to go there and how it is supposed to look.
  • 0:02 - the value on the left side of the colon is the field name. It specifies what replaces the replacement field - in this case it is an index, 0. The first argument of .format() will replace this replacement field.
  • 0:02 - the value on the right side of the colon specifies how it looks - the format spec. In this case it specifies a width of 2 characters and if the replacement only has one character to fill (from the left) with '0'.

Format String Syntax
Format Specification Mini-Language

wwii
  • 23,232
  • 7
  • 37
  • 77