Guide to String Formatting with Python
Table of Contents
String formatting is a robust and powerful part of any python programmer’s toolkit – nearly every piece of production software takes advantage of it in one way or another. The means of formatting strings, though, have greatly evolved over Python’s lifetime. From the %
formatting, to the format()
method, to formatted string literals, there’s no limit as to the potential of string crafting.
Composing the shape and content of strings in runtime is a basic capability of any high-level language. Whether you’re inserting variable values into a log message, composing output based on user input, or constructing messages to a client involving a timestamp – you’ll often need this function. At a basic level, you can concatenate strings simply using +
, but this is inefficient and also hard to make expressive. This is exactly where Python’s string formatting capabilities come in.
You can check out Kite’s Github repository to easily access the code from this post and others from their Python series.
The old way: printf
style formatting.
One of the initial ways of formatting strings in Python was based on the convention used by printf. Inserting a value into a string was done by representing it as a %, followed by a character indicating its type. So, to insert a string and an integer:
>>> this = "this"
>>> five = 5
>>> "%s is a %d" % (this, five)
'this is a 5'
%d
will throw a TypeError if the input is not an integer. %s
is the same as calling str()
on the input. This can be used with any object, not just strings:
>>> "%s is a list" % [1,2,3]
'[1, 2, 3] is a list'
%r
is the same as calling repr()
on the input, contrasting with %s
:
>>> "%s sounds like %r" % ("Seaweed", "Seaweed")
"Seaweed sounds like 'Seaweed'"
With floats, the number of digits displayed can be controlled by inserting that number:
>>> "%.3f" % 6.1234567
'6.123'
Note that when the digits are truncated, the value isn’t rounded. To add padding to strings, add the padding amount like this:
>>> for w in ['some', 'words', 'are', 'longer']:
... print("|%15s" % w)
...
| some
| words
| are
| longer
A dictionary can also be used to insert values into a string:
>>> ship_info = {'ship': 'personiples', 'captain': 'Archaeus'}
>>> "%(ship)s was run hard by %(captain)s" % ship_info
'personiples was run hard by Archaeus'
The main downsides of the printf style is that it’s very easy to introduce bugs, that it can be limited in how many arguments could be passed, and it’s also not as intuitive or expressive as more recent innovations.
Python 3: str.format()
With Python 3, a new way of formatting strings was introduced: the str.format()
method using the format specification mini-language that offered more powerful formatting. This was backported to Python 2.6.
This new specification uses curly brackets to indicate replacement fields rather than %s
In the %
style. The position of the arguments determines the position in the target string, which can be done with the str.format()
style as well: