str.format() creates a new string from a template. Curly braces mark placeholders, and positional arguments fill them in the order the placeholders appear. section 2.1.12
Insert values into a phrase
article = 'an'\nword = 'octopus'\nphrase = 'Ahoy, Captain, {} {} off the larboard bow!'.format(article, word)\nprint(phrase)\n# Ahoy, Captain, an octopus off the larboard bow!
Use formatting fields to control displayed values:
- Use a field width such as {:8} to align text or numbers into columns; text is left-justified and numbers right-justified. section 6.1.4
Choose str.format() or an f-string for many interpolation tasks. F-strings place variables or expressions directly inside the braces, while both forms support printf-style formatting options. section 2.3.4 section 6.5
Format generated output
A loop can select values, join adjective strings, and use str.format() to assemble repeated output such as insults with fixed punctuation and wording. section 9.1.5
>>> 'Pi is {:0.02f}'.format(math.pi)
'Pi is 3.14'
Compare unformatted and precision-controlled output for math.pi. section 6.1.4


