knowledge-kitchen / course-notes

Strings - Formatting

Examples of formatting strings in Python using any strings’ .format() function, the general built-in format() function, and the newer and more elegant f-string syntax (a form of syntactic sugar).

The basic idea

There are four different ways to create templated, formatted strings in Python…

You should be familiar with each, although f-strings are certainly easier to read and write.

Unnamed placeholders

first = 'a'
second = 'b'
third = 'c'
x = '{}, {}, {}'.format(first, second, third) # note the unnamed placeholders {}
print(x)
# 'a, b, c'

Numerical indices as placeholders (old fashioned syntax)

first = 'a'
second = 'b'
third = 'c'
x = '{0}, {1}, {2}'.format(first, second, third) # note the numerical indices as placeholders {0}, {1}, {2}
print(x)
# 'a, b, c'

Named placeholders (old fashioned syntax)

first = 'a'
second = 'b'
third = 'c'
x = '{foo}, {bar}, {baz}'.format(foo=first, bar=second, baz=third) # note the named placeholders {foo}, {bar}, {baz}... these are not variable names
print(x)
# 'a, b, c'

F-strings (simplified contemporary syntax… preferred!)

# variables holding values
first = 'a'
second = 'b'
third = 'c'
# inject the variables into a template string using the f-string syntax (note the 'f' prefix)
x = f'{first}, {second}, {third}' # note the direct variable name references
print(x)

Out of order placeholders

Numerical style (old fashioned syntax):

first = 'a'
second = 'b'
third = 'c'
x = '{2}, {1}, {0}'.format(first, second, third)
print(x)
# 'c, b, a'

F-string style (contemporary syntax):

first = 'a'
second = 'b'
third = 'c'
x = f'{third}, {second}, {first}'
print(x)
# 'c, b, a'

Repeating placeholders more than once

Numerical style (old fashioned syntax):

foo = 'abra'
bar = 'cad'
x = '{0}{1}{0}'.format(foo, bar)
print(x)
# 'abracadabra'

F-string style (contemporary syntax):

foo = 'abra'
bar = 'cad'
x = f'{foo}{bar}{foo}'
print(x)
# 'abracadabra'

Alignment

Left-aligned within 20 spaces

Using the format() function (old fashioned syntax):

foo = 'Harry'
x = format(foo, '<20s')
print(x)
# 'Harry               '

F-string style (contemporary syntax):

foo = 'Harry'
x = f'{foo:<20}'
print(x)
# 'Harry               '

Right-aligned within 20 spaces

Using the format() function (old fashioned syntax):

foo = 'Barry'
x = format(foo, '>20s')
print(x)
# '               Barry'

F-string style (contemporary syntax):

foo = 'Barry'
x = f'{foo:>20}'
print(x)
# '               Barry'

Center-aligned within 20 spaces

Using the format() function (old fashioned syntax):

foo = 'Harry'
x = format(foo, '^20s')
print(x)
# ' Harry '

F-string style (contemporary syntax):

foo = 'Barry'
x = f'{foo:^20}'
print(x)
# '       Barry        '

Right-aligned within 20 spaces with the '*' as a fill character

Using the format() function (old fashioned syntax):

foo = 'Harry'
x = format(foo, '*>20s')
print(x)
# '***************Harry'

F-string style (contemporary syntax):

foo = 'Harry'
x = f'{foo:*>20}'
print(x)
# '***************Barry'

Center-aligned within 20 spaces with the '-' as a fill character

Using the format() function (old fashioned syntax):

foo = 'Barry'
x = format(foo, '-^20s')
print(x)
# '-------Barry--------'

F-string style (contemporary syntax):

foo = 'Barry'
x = f'{foo:-^20}'
print(x)
# '-------Barry--------'

Combining formatted strings

A string left-aligned within 20 spaces, combined with another string left-aligned within 10 spaces

Using the format() function (old fashioned syntax):

x = format('Harry', '<20s')
y = format('$20', '<10s')
z = "{0}{1}".format(x, y)
print(z)
# 'Harry               $20       '

F-string style (contemporary syntax):

x = f'{"Harry":<20}'
y = f'{"$20":<10}'
z = f'{x}{y}'
print(z)
# 'Harry               $20       '

Limiting decimal places

Two decimal places

Using the format() function (old fashioned syntax):

x = format(0.33333333, '.2f')
print(x)
# '0.33'

Five decimal places:

Using the format() function (old fashioned syntax):

x = format(0.33333333, '.5f')
print(x)
# '0.33333'

F-string style (contemporary syntax):

x = f'{0.33333333:.5f}'
print(x)
# '0.33333'

Commas for thousands

Using the format() function (old fashioned syntax):

x = format(33333333.22222222, ',.5f')
print(x)
# '33,333,333.22222'

F-string style (contemporary syntax):

x = f'{33333333.22222222:,.5f}'
print(x)
# '33,333,333.22222'

Commas for thousands, right-aligned

Using the format() function (old fashioned syntax):

x = format (33333333.22222222, '>20,.2f')
print(x)
# '       33,333,333.22'

F-string style (contemporary syntax):

x = f'{33333333.22222222:>20,.2f}'
print(x)
# '       33,333,333.22'

Percentage conversion

Basic percentage conversion

Using the format() function (old fashioned syntax):

x = format(0.52, '%')
print(x)
# '52.000000%'

F-string style (contemporary syntax):

x = f'{0.52:%}'
print(x)
# '52.000000%'

Percentage conversion with limited decimal places

Using the format() function (old fashioned syntax):

x = format(0.52, '.2%')
print(x)
# '52.00%'

F-string style (contemporary syntax):

x = f'{0.52:.2%}'
print(x)
# '52.00%'

Percentage conversion with no decimal places

Using the format() function (old fashioned syntax):

x = format(0.52, '.0%')
print(x)
# '52%'

F-string style (contemporary syntax):

x = f'{0.52:.0%}'
print(x)
# '52%'

Left-aligned with percentage conversion and limited decimal places

Using the format() function (old fashioned syntax):

x = format(0.52, '<20.2%')
print(x)
# '52.00%              '

F-string style (contemporary syntax):

x = f'{0.52:<20.2%}'
print(x)
# '52.00%              '

Integer numbers with commas

An integer, with commas for thousands

Using the format() function (old fashioned syntax):

x = format(5200, ',d')
print(x)
# '5,200'

F-string style (contemporary syntax):

x = f'{5200:,d}'
print(x)
# '5,200'

An integer, with commas for thousands, left-aligned within 20 spaces

Using the format() function (old fashioned syntax):

x = format(5200, '<20,d')
print(x)
# '5,200               '

F-string style (contemporary syntax):

x = f'{5200:<20,d}'
print(x)
# '5,200               '

An integer, with commas for thousands, right-aligned within 20 spaces

Using the format() function (old fashioned syntax):

x = format(5200, '>20,d')
print(x)
# '               5,200'

F-string style (contemporary syntax):

x = f'{5200:>20,d}'
print(x)
# '               5,200'