June 2017
Beginner
352 pages
8h 39m
English
One of the most interesting and frequently used string methods is format(). This supersedes, although does not replace, the string interpolation technique used in older versions of Python, and which we do not cover in this book. The format() method can be usefully called on any string containing so-called replacement fields which are surrounded by curly braces. The objects provided as arguments to format() are converted to strings and used to populate these fields. Here's an example:
>>> "The age of {0} is {1}".format('Jim', 32)'The age of Jim is 32'
The field names, in this case 0 and 1, are matched up with the positional arguments to format(), and each argument is converted to a string behind the scenes.
A field name may ...
Read now
Unlock full access