String Formatting

Customizing the user experience or generating well-formed program output will almost always result in manipulating strings. This usually involves combining or substituting strings. For example, to concatenate (combine) strings in Python, you could simply use the addition operator +:

In [3]:
"cat" + " " + 'dog'
Out[3]:
'cat dog'

However, the addition operator in Python is inefficient when it comes to strings. In some cases, especially when the operation is only done a few times, it does not directly impact the program, but when the code is executed numerous times, contains a large number of string operations, or has a long running accumulator of strings, there is a better option available.

In [4]:
"{} {}".format('cat', 'dog')
Out[4]:
'cat dog'

The string format method is an easy and efficient way to format numbers, concatenate strings, and substitute strings. The format method is part of the string data type. The format method looks for pairs of curly brackets {} and plugs in values that are passed into the function.

In [5]:
'{:.2f}'.format(2/3)
Out[5]:
'0.67'

The above example plugs the result of '2/3' in place of the curly brackets of the string. In this case, {:.2f} formats the value substituted as a floating point number with two decimal places. Note that it also takes care of rounding! There are many other options to format numbers that can be found in the Python docs. Below is an example of the percentage format syntax.

In [6]:
'{:.2%}'.format(.1)
Out[6]:
'10.00%'

The format method isn't limited to formatting single values. It accepts any number of arguments. These are positional arguments, meaning they are processed in order as they are given.

In [7]:
'{:.2f}'.format(1,2,3,4)
Out[7]:
'1.00'

In the above example, only the number 1 is plugged into the string since it only contains a single set of curly brackets. The rest of the arguments/values are ignored. We can add more sets of curly brackets to utilize more of the values:

In [10]:
'{:.2f}, {:.2f}'.format(1,2,3,4)
Out[10]:
'1.00, 2.00'
In [9]:
'{:.2f}, {:.2f}, {:.2f}, {:.2f}, {:.2f}'.format(1,2,3,4)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-9-c7a08fd8eb0e> in <module>()
----> 1 '{:.2f}, {:.2f}, {:.2f}, {:.2f}, {:.2f}'.format(1,2,3,4)

IndexError: tuple index out of range

If you have more sets of curly brackets than arguments, you will get an exception as seen in the above example. However, you can have more sets of brackets than values if you specify which values to substitute for each set. For example:

In [11]:
'{0:.2f}, {0:.2f}'.format(1)
Out[11]:
'1.00, 1.00'

The bracket syntax is {field name:format string}. The left side of the : can be specified using either the index of the value or name of the value. The index is the position of the value as originally passed into the format method. The indices start counting at 0 not 1. The right side of the : is reserved for the format string. .2f is an example of a format string.

In [20]:
'{3:.2f}, {2:.2f}'.format(1,2,3,4)
Out[20]:
'4.00, 3.00'
In [21]:
'{3:.2f}, {1:.2f}'.format(1,2,3,4)
Out[21]:
'4.00, 2.00'
In [12]:
'{3:.2f}, {0:.2f}'.format(1,2,3,4)
Out[12]:
'4.00, 1.00'

You don't have to specify a format string, just like you don't have to specify a position.

In [13]:
'{3}, {0}'.format(1,2,3,4)
Out[13]:
'4, 1'
In [14]:
'{}, {}'.format(1,2,3,4)
Out[14]:
'1, 2'

The format method isn't restricted to only formatting numbers. It's very useful for customizing or personalizing messages:

In [16]:
"That dog's name is {0}. {0}'s favorite food is {1}.".format('Max', 'steak')
Out[16]:
"That dog's name is Max. Max's favorite food is steak."

You can also use escape characters in strings that you use format on; however, you cannot escape the curly brackets themselves. If you want to put curly brackets into a string you are formatting, you will need to put them in the value you are substituting:

In [17]:
"This string \"has\" brackets {}".format("{hi}")
Out[17]:
'This string "has" brackets {hi}'
In [18]:
"This string \"has\" doesn't work \{{}\}".format("hi")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-25da2e2ef74c> in <module>()
----> 1 "This string \"has\" doesn't work \{{}\}".format("hi")

ValueError: Single '}' encountered in format string