Formatting Strings
We'll cover the following...
Let’s take another look at humansize.py:
① 'KB', 'MB', 'GB'… those are each strings.
② Function docstrings are strings. This docstring spans multiple lines, so it uses three-in-a-row quotes to start and end the string.
③ These three-in-a-row quotes end the docstring.
④ There’s another string, being passed to the exception as a human-readable error message.
⑤ There’s a… whoa, what the heck is that?
Strings can be defined with either single or double quotes.
Python 3 supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert a value into a string with a single placeholder.
① No, my password is not really PapayaWhip.
② There’s a lot going on here. First, that’s a method call on a string literal. Strings are objects, and objects have methods. Second, the whole expression evaluates to a string. Third, {0} and {1} are replacement fields, which are replaced by the arguments passed to the format() method.
Compound field names
The previous example shows the simplest case, where the replacement fields are simply integers. Integer replacement fields are treated as positional indices into the argument list of the format() method. That means that {0} is replaced by the ...