Back

An introduction to formatting data

Up to now, we haven't talked very much about formatting your work, laying it out so that it displays nicely looking like the way that you want it to look. You might want data to be in tables. You might want floating point numbers to have a certain number of decimal places. Python 3 makes this task very easy by using a string method called format, which you can pass arguments to. Now that might sound highly complex, but once you start using it, you will see how easy it actually is!

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))

What happened? It should have printed 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?
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))

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))

What happened? 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. Suppose you wanted the number to be left hand justified. How could you do this?
Q6.
Suppose your name wasn't Jack but Jackabitheonaris and the field width was set to 10. What do you think will happen when you run the program? Run it and see. Were you right?
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))

What happened? 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. Try changing 3f to 5f or 10f or -2f. What happened?
Q8. Can you predict what happens when 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))?

Try it and see. Were you correct? Explain what happened.
Q9. 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))

What happened now?
Q10. f stands for decimal places. What would you use if you wanted to indicate that a number was an integer? Write a line of code that displays the number 3 so that it is in a field five spaces wide and formatted as an integer.

Back