## String formatting¶

We often need to 'print' variables to the screen or to a file. When we are experimenting with things on our own, we may not care about how our output looks, but when we are sharing our work with others, we want things to be neat and easily readable.

This self-guided notebook takes you through the basics of string formatting using the newer python str.format() way of formating strings. String formatting can get very sophisticated, but we need only a few simple things. As usual, you can always Google for 'python string format' for more.

[There are two other ways to format strings in python. One uses the \% sign and follows from older c-style formatting. The newest are called 'f-strings'. The old-style formatting will show up sometimes, but using str.format is preferred. f-strings are good, too, but we'll stick with str.format for this course. ]

### The basics¶

We insert variables into a string using the str.format() method.

In [16]:
school_song = 'Varsity'

print( 'The UW school song is {0}.'.format(school_song) )

The UW school song is Varsity.


The 0 in {0} is the positional argument. It says to insert the 0-th (first) thing in format() in place of {0}.

Let's try this with two things to be printed.

In [49]:
year_written = 1898 # What kind of variable is this?

print( 'The UW school song is {0}. It was written in {1}.'.format(school_song, year_written) )

# The values can be outputed in any order.
print( 'The UW school song is {1}. It was written in {0}.'.format(school_song, year_written) )

# The values can be used more than once.
print( 'The UW school song is {0}. It was written in {1}. The first lyric is {0}! {0}!'.format(school_song, year_written) )

The UW school song is Varsity. It was written in 1898.
The UW school song is 1898. It was written in Varsity.
The UW school song is Varsity. It was written in 1898. The first lyric is Varsity! Varsity!


Notice that the format function handled converting the int in year_written to a string.

### Named placeholders¶

Instead of numbers, the variables to print can be given names.

In [43]:
print( 'The UW school song is {song}. It was written in {year}.'.format(song='Varsity', year='1998') )

The UW school song is Varsity. It was written in 1998.


### Formatting floats¶

For us, string formatting shows up most often when we want to format the output of numbers. This includes dealing with significant digits, currencies, dates, and alignment.

In [31]:
foo = 1/3
print(foo)

# The default float format
print('foo = {0:f}'.format(foo))

0.3333333333333333
foo = 0.333333


The syntax is {position:format}. We have already seen how the position argument works. The format argument is a code that tells format() how to, well, format the value.

In this case, we used the f to specify the float format. Doing so printed only three trailing digits. We can control the trailing digits and the overall width of the float, too.

In [32]:
print('foo = {0:5.3f}'.format(foo))

foo = 0.333


The code 5.3f means: output the number in 5 'spaces' overall with 3 of the 5 spaces to the right of the decimal point.

In [35]:
# Now the space reserved for the number is 8
print('foo = {0:8.3f}'.format(foo))

# The zero before the 8 prints out the leading zeros.
print('foo = {0:08.3f}'.format(foo))

foo =    0.333
foo = 0000.333

In [39]:
big_float = 123456.789

print('the big float is = {0:5.2f}'.format(big_float))

the big float is = 123456.79


We didn't specify a big enough window to fit the number, so space was added, but the 2 digits to the right of the decimal was still respected.

If we add a comma to the left of the decimal point 5,.2f we get commas to group digits.

In [45]:
print('An easier to read  big float is = {0:5,.2f}'.format(big_float))

An easier to read  big float is = 123,456.79


### Other formats¶

There are lots of things you can do to format strings. We won't need most of them.

You might look into: {:.2%} for percentages and {:.2e} for exponential notation. Insert a cell below and practice!