liveBook · Manning

Format string

As explained in The Quick Python Book, Fourth Edition

A format string is a string containing replacement fields that specify how supplied values are incorporated into formatted text.

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

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through field selection, escaped braces, alignment, padding, and dynamic width; defers the full format-specifier mini-language to standard-library documentation.
Examples
Compares short Python expressions that substitute words, escape braces, adjust padding, format decimals, and describe a binary record.
What it assumes
Assumes familiarity with Python strings, method calls, and arguments; introduces replacement-field syntax through small examples.
What you can do after
Apply it via `str.format()` to select and display values with basic alignment and padding.
How it connects
Connects `str.format()` to f-strings, percent formatting, keyword arguments, and `struct` binary packing.

Also explained in

Compare how 5 books explain format string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.