String formatting
You will need to know a few things about string formatting to follow along with this book's examples. The topic itself can get very deep, but we will just stick to some basics.
There are two types of string formatting in Python. The older and still popular version uses the percent character (%
), while the newer and more flexible version uses the format
method on strings. Here is a very basic example of each.
>>> name = 'Jon' >>> 'Hi, %s!' % name 'Hi, Jon!' >>> 'Hi, {0}!'.format(name) 'Hi, Jon!'
We use the %
version exclusively in this book because it is more concise. I would encourage you to use the format
method for new code. Translating this book's examples should be straightforward.
Inside the string being formatted, the %
character indicates something that will be replaced, and the characters following the %
specify how it will be replaced. For example, consider the difference between...