Online Book Reader

Home Category

Learning Python - Mark Lutz [119]

By Root 1671 0
the use of *parts here to unpack a tuple's items into individual arguments, as we did on Page 132 when studying fractions; more on this in Chapter 18):

>>> somelist = list('SPAM')

>>> somelist

['S', 'P', 'A', 'M']

>>> 'first={0[0]}, third={0[2]}'.format(somelist)

'first=S, third=A'

>>> 'first={0}, last={1}'.format(somelist[0], somelist[-1]) # [-1] fails in fmt

'first=S, last=M'

>>> parts = somelist[0], somelist[-1], somelist[1:3] # [1:3] fails in fmt

>>> 'first={0}, last={1}, middle={2}'.format(*parts)

"first=S, last=M, middle=['P', 'A']"

Adding Specific Formatting

Another similarity with % expressions is that more specific layouts can be achieved by adding extra syntax in the format string. For the formatting method, we use a colon after the substitution target’s identification, followed by a format specifier that can name the field size, justification, and a specific type code. Here’s the formal structure of what can appear as a substitution target in a format string:

{fieldname!conversionflag:formatspec}

In this substitution target syntax:

fieldname is a number or keyword naming an argument, followed by optional “.name” attribute or “[index]” component references.

conversionflag can be r, s, or a to call repr, str, or ascii built-in functions on the value, respectively.

formatspec specifies how the value should be presented, including details such as field width, alignment, padding, decimal precision, and so on, and ends with an optional data type code.

The formatspec component after the colon character is formally described as follows (brackets denote optional components and are not coded literally):

[[fill]align][sign][#][0][width][.precision][typecode]

align may be <, >, =, or ^, for left alignment, right alignment, padding after a sign character, or centered alignment, respectively. The formatspec also contains nested {} format strings with field names only, to take values from the arguments list dynamically (much like the * in formatting expressions).

See Python’s library manual for more on substitution syntax and a list of the available type codes—they almost completely overlap with those used in % expressions and listed previously in Table 7-4, but the format method also allows a “b” type code used to display integers in binary format (it’s equivalent to using the bin built-in call), allows a “%” type code to display percentages, and uses only “d” for base-10 integers (not “i” or “u”).

As an example, in the following {0:10} means the first positional argument in a field 10 characters wide, {1:<10} means the second positional argument left-justified in a 10-character-wide field, and {0.platform:>10} means the platform attribute of the first argument right-justified in a 10-character-wide field (note the use of dict() to make a dictionary from keyword arguments, introduced in Chapter 4 and covered in Chapter 8):

>>> '{0:10} = {1:10}'.format('spam', 123.4567)

'spam = 123.457'

>>> '{0:>10} = {1:<10}'.format('spam', 123.4567)

' spam = 123.457 '

>>> '{0.platform:>10} = {1[item]:<10}'.format(sys, dict(item='laptop'))

' win32 = laptop '

Floating-point numbers support the same type codes and formatting specificity in formatting method calls as in % expressions. For instance, in the following {2:g} means the third argument formatted by default according to the “g” floating-point representation, {1:.2f} designates the “f” floating-point format with just 2 decimal digits, and {2:06.2f} adds a field with a width of 6 characters and zero padding on the left:

>>> '{0:e}, {1:.3e}, {2:g}'.format(3.14159, 3.14159, 3.14159)

'3.141590e+00, 3.142e+00, 3.14159'

>>> '{0:f}, {1:.2f}, {2:06.2f}'.format(3.14159, 3.14159, 3.14159)

'3.141590, 3.14, 003.14'

Hex, octal, and binary formats are supported by the format method as well. In fact, string formatting is an alternative to some of the built-in functions that format integers to a given base:

>>> '{0:X}, {1:o}, {2:b}'.format(255, 255, 255) # Hex, octal, binary

'FF, 377, 11111111'

>>> bin(255), int('11111111', 2), 0b11111111

Return Main Page Previous Page Next Page

®Online Book Reader