Python string format examples

The format method for Python strings (introduced in 2.6) is very flexible and powerful.  It’s also easy to use, but the documentation is not very clear.  It all makes sense with a few examples.  I’ll start with one and add more as I have time:

Formatting Numbers in Python Strings

Formatting a floating-point number


The result is the following string:


Braces { } are used to enclose the “replacement field”
0 indicates the first argument to method format
: indicates the start of the format specifier
.4 indicates four decimal places
f indicates a floating-point number

Align floating-point numbers at the decimal point

There is no format specifier that aligns numbers at the decimal point. Decimal-point alignment is accomplished by fixing the field width, which is set by placing a number before the decimal point in the format string. Compare the previous example to the output shown below:



'  0.9877'
' 10.9877'

Scientific Notation




Format decimal fraction as percentage

Pass in a decimal fraction; the fraction will be multiplied by 100 and the percent sign will be added for you:

"{:.1%} of programmers prefer Python".format(0.473726)

Result (note correct rounding behavior):

47.4% of programmers prefer Python

Leave space for a minus sign

"{: .4e}".format(0.098765)
"{: .4e}".format(-0.1234567)



Note that there is a space between the colon and the dot in the format specifier. The decimal points for the two numbers are aligned in the output, which is handy for printing tabular data.

Text String Formatting

Tabular text output

Sometimes you need to write out multiple lines in a table-like format. The easiest way to do that is by defining a format string with fixed widths for each column. If you define the format string outside of your print statements, you don’t have to worry about updating multiple format strings every time you tweak the column widths.

listFormatString = '{:32}  {:24}  {:24}'
print(listFormatString.format('Name', 'Created', 'Updated'))
for item in items:
    print(listFormatString.format(item['name'], item['createdTime'], item['modifiedTime']))


Multiple Arguments

In Python 2.6 you can include multiple arguments like this:

"sin({0:.4f}) = {1:.4e}".format(0.1234567890, sin(0.123456789))
'sin(0.1235) = 1.2314e-01'

In Python 2.7 and 3.x, you may omit the first integer from each replacement field, and the arguments to format will be taken in order:

"sin({:.4f}) = {:.4e}".format(0.1234567890, sin(0.123456789))
'sin(0.1235) = 1.2314e-01'

2 thoughts on “Python string format examples

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.