Exercises: Format Strings

4567, 10000, 12345.67)and produce:’file_002 : 123.46, 1.00e 04, 1.23e 04′
Let’s look at each of the four tuple elements in turn:
The first element is used to generate a filename that can help with file sorting. The idea behind the “file_002” is that if you have a bunch of files that you want to name with numbers that can be sorted, you need to “pad” the numbers with zeros to get the right sort order.
To illustrate this further let’s look at an example:
In [10]: fnames = [‘file1’, ‘file2’, ‘file10’, ‘file11’]
In [11]: fnames.sort()
In [12]: fnames
Out[12]: [‘file1’, ‘file10’, ‘file11’, ‘file2’]
That is probably not what you wanted. However:
In [1]: fnames = [‘file001’, ‘file002’, ‘file010’, ‘file011’]
In [3]: sorted(fnames)
Out[3]: [‘file001’, ‘file002’, ‘file010’, ‘file011’]
That works!
So you need to find a string formatting operator that will “pad” the number with zeros for you.
The second element is a floating point number. You should display it with 2 decimal places shown.
The third value is an integer, but could be any number. You should display it in scientific notation, with 2 decimal places shown.
The fourth value is a float with a lot of digits – display it in scientific notation with 3 significant figures.
Task Two
Using your results from Task One, repeat the exercise, but this time using an alternate type of format string (hint: think about alternative ways to use .format() (keywords anyone?), and also consider f-strings if you’ve not used them already).