An introduction to formatting data - Answers
Q1. Create a new Python program. Type this in, save it and run it.
print("My name is Jack and I am 14 years old.")
This should be very familiar to you. The keyword print took a string object and printed it out.
Q2. Modify the above code and then run it.
print("My name is {} and I am {} years old.".format("Jack",14))
The code prints out the same thing. Strings are objects. Objects have methods (methods are just bits of code that carry out a very specific job). String objects have lots of methods and one of them is the format method. After the string finished, we put a dot then the name of the method and then in brackets, any arguments (actual pieces of data) we want to pass to the format method. In the above, you can see we attached this:
.format("Jack",14)
to the end of the string. This might seem a waste of time. Why not just do it like we did in Q1 - because you have a lot more flexibility in the way things are laid out using the format method.
Q3. Modify your code and run it again. Try each of these:
print("My name is {0} and I am {0} years old.".format("Jack",14))
print("My name is {1} and I am {0} years old.".format("Jack",14))
print("My name is {0} and I am {1} years old.".format("Jack",14))
print("My name is {1} and I am {1} years old.".format("Jack",14))
produces:
My name is Jack and I am Jack years old.
My name is 14 and I am Jack years old.
My name is Jack and I am 14 years old.
My name is 14 and I am 14 years old.
What happened? The first piece of data ("Jack") in the format brackets is 0 and the second piece of data (14) is 1. By inserting 0 and 1 in the curly brackets in the string, we can put any piece of data we like in any position we like in the string.
Q4. Modify your code and run it again:
print("My name is {0:10} and I am {1:5} years old.".format("Jack",14))
The data is spaced out to a minimum of the field width specified after the colon. You can pass more arguments to the curly brackets. For example, suppose you want to space out the name position so that 10 characters of space are allowed. You put a colon after the zero and then the number of spaces you want to allow. Notice that strings ("Jack" is a string) are left hand justified so any extra space after Jack are padded out with spaces.
You can do the same with numbers. By putting a colon and then 5 after the 1 in curly brackets, you are telling Python to ensure there are 5 spaces for the number. Note that numbers are always right hand justified. If there aren't five digits in the number, extra spaces in front of the number will be added to pad out the position to the desired width.
Q5. If you wanted the number to be left hand justified, (one way is to) convert it into a string first:
print("My name is {0:10} and I am {1:5} years old.".format("Jack",str(14)))
Q6. If your name wasn't Jack but Jackabitheonaris and the field width was set to 10, the whole name would still be printed. The field width you set is the minimum width for the field.
Q6. Modify your code so that it now looks like this:
print("My name is {0:10} and I am {1:5.3f} years old.".format("Jack",14))
We have put a dot followed by 3f in the curly brackets for the number. 3f tells Python to format the number so it has 3 decimal places.
Q7. If you try changing 3f to 5f or 10f or -2f, you will adjust the width of the field. Spaces are used to pad out the width. Negaitve numbers will cause an error as you can't have a negative field width.
Q8. If you add a dot and 3f to the string field for Jack, like this:
print("My name is {0:10.3f} and I am {1:5.3f} years old.".format("Jack",14))?
you will get an error. Strings can't have decimal places, only numbers can.
Q9. If you modify Q8 so that you leave out the f for the name field e.g.
print("My name is {0:10.3} and I am {1:5.3f} years old.".format("Jack",14))
or
print("My name is {0:10.1} and I am {1:5.3f} years old.".format("Jack",14))
then the string only displays the number of characters specified.
Q10. f stands for decimal places. You use d to indicate that a number was is an integer.
print("{0:5d}".format(3))