A quick guide to str.
format()
Paul McKeown
August 10, 2013
This example driven guide covers the most useful formatting commands in
Python’s [Link]() mini language.
1 Basic insertion
The most basic use for format is to insert various variables into a string. Note,
the format method will automatically covert items to strings.
>>> name = ’Paul’
>>> weight = 110
>>> ’{} weighs {}’.format(name, weight)
’Paul weighs 110’
The above example uses implicit indexing, ie, the items that are simply inserted
in the order they are specified in the format parameters. The following example
would be equivalent.
>>> ’{0} weighs {1}’.format(name, weight)
’Paul weighs 110’
To see how the indexing works, think about the difference between the pre-
vious and following example:
>>> "{1} is {0}’s weight.".format(name, weight)
’110 is Paul’s weight.’
Adding strings together is a viable alternative to format but it requires
making all of the items ito strings, e.g., you would need to do the following:
>>> name + ’ weighs ’ + str(weight)
’Paul weighs 110’
But this can get tedious for multiple items with various strings between them,
not to mention the possibility of having to use str() a number of times.
Besides just inserting items into a string, the format method can be used
to set the width of those items, the alignment of the items, and the numeric
representation (e.g., number of decimal places). This makes the format method
the best way to format more complicated output. The following sections cover
the most important of these.
In general, to format an inserted item we put a colon followed by formatting
settings inside the {} for that item. If the item is indexed then we put the :
after the index.
1
2 Item width
To set the width of items we can do the following (spaces are drawn as ):
>>> name = ’Paul’
>>> weight = 110
>>> ’{:20}|{:5}’.format(name, weight)
’Paul | 110’
>>> ’{0:20}|{1:5}’.format(name, weight)
’Paul | 110’
The | character is put in to highlight the two sections (making the output string
20 + 1 + 5 = 26 characters wide); if this was left out there would be no gap
between the items (and the total width of the string would be 20 + 5 = 25).
Notice that the name is left-aligned with the rest of the width (padded with
spaces) and the number is right-aligned. By default the format method will
left-align strings and right-align numbers. Compare the above example to the
following:
>>> ’{0:20}|{1:5}’.format(name, str(weight))
’Paul |110 ’
Note that the width setting specifies minimum width. Be careful with strings
that are wider than the minimum – they will take up more space and this might
cause havoc when formatting your columns. For example:
>>> name = ’Paul with a longer than usual name’
>>> weight = 110
>>> ’{0:20}|{1:5}’.format(name, weight)
’Paul with a longer than usual name| 110’
3 Item alignment
The three most common alignment settings that you will use are left (<), right
(>) and centre (ˆ). The alignment setting is added before the width value. By
default the space character is used to pad out to the set width. See the examples
below:
>>> name = ’Paul’
>>> weight = 110
>>> ’{:>20}|{:<5}’.format(name, weight)
’ Paul|110 ’
>>> ’{0:>20}|{1:>5}’.format(name, weight)
’ Paul| 110’
>>> ’{0:^20}|{1:5}’.format(name, weight)
’ Paul | 110’
The last example shows that we don’t really need to right-align numbers as
they will be right-aligned by default. Similarly it is not necessary to left-align
strings.
2
4 Decimal places
The most useful numerical formatting option is setting the number of decimal
places that will be displayed. To do this you simply add .nf after the width
setting (or after the : if you are not setting the width of the item). The n
indicates the number of decimal places and the f indicates you want a floating
point value.
>>> name = ’Paul’
>>> weight = 110.2353
>>> ’{:>20}|{:5.1f}’.format(name, weight)
’ Paul|110.2’
>>> ’{0:>20}|{1:10.2f}’.format(name, weight)
’ Paul| 110.24’
>>> ’{0:>20}|{1:5.3f}’.format(name, weight)
’ Paul|110.235’
Notice that the number is 7 characters wide in the last example. Remember
the width setting sets the minimum width and anything longer than this will
expand as necessary.
That’s about all you need to know about [Link]() for COSC121. The
best way to get a feel for how to use [Link]() is to play around in the
command line - type in some format commands and see if you can predict the
resulting string.