Online Book Reader

Home Category

Learning Python - Mark Lutz [121]

By Root 1630 0
3.14159)

'3.141590, 3.14, 003.14'

# Hex and octal, but not binary

>>> '%x, %o' % (255, 255)

'ff, 377'

The format method has a handful of advanced features that the % expression does not, but even more involved formatting still seems to be essentially a draw in terms of complexity. For instance, the following shows the same result generated with both techniques, with field sizes and justifications and various argument reference methods:

# Hardcoded references in both

>>> import sys

>>> 'My {1[spam]:<8} runs {0.platform:>8}'.format(sys, {'spam': 'laptop'})

'My laptop runs win32'

>>> 'My %(spam)-8s runs %(plat)8s' % dict(spam='laptop', plat=sys.platform)

'My laptop runs win32'

In practice, programs are less likely to hardcode references like this than to execute code that builds up a set of substitution data ahead of time (to collect data to substitute into an HTML template all at once, for instance). When we account for common practice in examples like this, the comparison between the format method and the % expression is even more direct (as we’ll see in Chapter 18, the **data in the method call here is special syntax that unpacks a dictionary of keys and values into individual “name=value” keyword arguments so they can be referenced by name in the format string):

# Building data ahead of time in both

>>> data = dict(platform=sys.platform, spam='laptop')

>>> 'My {spam:<8} runs {platform:>8}'.format(**data)

'My laptop runs win32'

>>> 'My %(spam)-8s runs %(platform)8s' % data

'My laptop runs win32'

As usual, the Python community will have to decide whether % expressions, format method calls, or a toolset with both techniques proves better over time. Experiment with these techniques on your own to get a feel for what they offer, and be sure to see the Python 2.6 and 3.0 library manuals for more details.

* * *

Note


String format method enhancements in Python 3.1: The upcoming 3.1 release (in alpha form as this chapter was being written) will add a thousand-separator syntax for numbers, which inserts commas between three-digit groups. Add a comma before the type code to make this work, as follows:

>>> '{0:d}'.format(999999999999)

'999999999999'

>>> '{0:,d}'.format(999999999999)

'999,999,999,999'

Python 3.1 also assigns relative numbers to substitution targets automatically if they are not included explicitly, though using this extension may negate one of the main benefits of the formatting method, as the next section describes:

>>> '{:,d}'.format(999999999999)

'999,999,999,999'

>>> '{:,d} {:,d}'.format(9999999, 8888888)

'9,999,999 8,888,888'

>>> '{:,.2f}'.format(296999.2567)

'296,999.26'

This book doesn’t cover 3.1 officially, so you should take this as a preview. Python 3.1 will also address a major performance issue in 3.0 related to the speed of file input/output operations, which made 3.0 impractical for many types of programs. See the 3.1 release notes for more details. See also the formats.py comma-insertion and money-formatting function examples in Chapter 24 for a manual solution that can be imported and used prior to Python 3.1.

* * *

Why the New Format Method?

Now that I’ve gone to such lengths to compare and contrast the two formatting techniques, I need to explain why you might want to consider using the format method variant at times. In short, although the formatting method can sometimes require more code, it also:

Has a few extra features not found in the % expression

Can make substitution value references more explicit

Trades an operator for an arguably more mnemonic method name

Does not support different syntax for single and multiple substitution value cases

Although both techniques are available today and the formatting expression is still widely used, the format method might eventually subsume it. But because the choice is currently still yours to make, let’s briefly expand on some of the differences before moving on.

Extra features

The method call supports a few extras that the expression does not,

Return Main Page Previous Page Next Page

®Online Book Reader