A format string is a string with places for values to be inserted. In Python 3, `str.format()` fills replacement fields marked by braces: `"{0} is the {1}".format("Ambrosia", "food")` produces `"Ambrosia is the food"`. Double braces to include literal braces, as in `"{{Ambrosia}}".format()`, which produces `"{Ambrosia}"`. section 6.6 section 6.6.1
Choose fields and formatting options to suit the output:
- Numbered fields such as `{0}` and `{1}` select arguments by position. A field can appear more than once or in a different order. section 6.6.1
- Named fields select keyword arguments: `"{food} is the food of gods".format(food="Ambrosia")`. section 6.6.3
- A specifier after `:` controls the result. For example, `{0:>10}` right-aligns a value in a ten-character field; `{0:&>10}` uses `&` for padding. Width can also come from another argument, as in `{0:{1}}`. section 6.6.3
"{0:10} is the food of gods".format("Ambrosia") #1
'Ambrosia is the food of gods'
"{0:{1}} is the food of gods".format("Ambrosia", 10) #2
'Ambrosia is the food of gods'
"{food:{width}} is the food of gods".format(food="Ambrosia", width=10)
'Ambrosia is the food of gods'
"{0:>10} is the food of gods".format("Ambrosia") #3
' Ambrosia is the food of gods'
"{0:&>10} is the food of gods".format("Ambrosia") #4
'&&Ambrosia is the food of gods'
#1 :10 makes the field 10 spaces wide, padded with spaces.
#2 :{1} width is from the second parameter.
#3 :>10 forces right justification.
#4 :&>10 forces right justification padded with & character.
Compare the outputs to see how field width, right alignment, a custom fill character, and argument-supplied width change the same value. section 6.6.3
Other Python text-formatting forms serve similar jobs. An f-string puts expressions directly inside braces, without a `.format()` call, and accepts the same format specifiers shown here. Older `%` formatting can take named values from a dictionary: `"%(PI).2f" % {"PI": 3.14159}` produces `"3.14"`. Named values are useful when many substitutions would make positional order hard to track. section 6.7 section 6.8.2
A `struct` format string has a different purpose: it describes fields in packed binary data, not replacement fields in text. For example, `hd7s` specifies a C short integer, a C double, and a seven-character string for `struct.pack()` or `struct.unpack()`. Check which formatting operation will read the string before interpreting its syntax. section 13.7




