We call the format method on a piece of text and give it the number, or name of the variable, that we want to format. The formatted number appears in the text in place of the curly braces. Like this:
print("A spider has {} legs, whilst an insect has {} legs".format(8,6))
The first set of curly braces is replaced by the first item in the format method's brackets. The next { } is filled by the next thing in the brackets and so on.
By putting a number in the curly braces you can re-use the same variable in the same piece of text:
name = "triangle"
sides = 3
total_angle = 180
print("A {0} has {1} sides and {1} angles. The {1} angles add up to {2} degrees".format(name, sides, total_angle))
***NB: The numbering starts at 0. The same as range() or lists.
print("Pi to 5 decimal places is {0:.5f}".format(3.141592653589793 ))
The {0:.5f} bit tells the format method to use the first (zeroth - because computers count from zero) number in its bracket and format it to 5 decimal places. The f stands for 'floating point format'. If we used {3:.2f} the format method would replace it with the 4th item in its brackets, formatted to 2 decimal places.
The format method returns a string version of the data, formatted as required. If the thing we are formatting is already text then we don't need to specify any format, we can just use the position number to tell the format method which text to use.
print("The book {1} by {0} costs ${2:.2f}".format("Neal Stephenson", "Snow Crash", 32.45435))
*** Note how money can now always be printed with 2 decimal places and right next to the dollar sign ***
This makes printing stuff out from your programs so much easier. You don't have to use " and , to include a variable in your print. You can use the same data several times in the same print line by using the same position number. You can replace the items in the format method's brackets with variables for even more flexibility.
height_cm = float(input("How tall are you in cm ? "))
height_inches = height_cm / 2.54
print("{0:.0f} cm is {1:.1f} inches".format(height_cm, height_inches))
Computers are often used in scientific fields where the numbers can get very big (number of stars in the known universe roughly 1,000,000,000,000,000,000,000,000) or very small (Planck length 0.00000000000000000000000000000000001616 metres) and so displaying them to set number of decimal places is not a good idea. So we use significant figures or the 'general format' and we use g in our curly braces instead of f.
print("1 divided by 30 is {0:.4g} to 4 significant figures".format(1/30))
will print 1 divided by 30 is 0.03333 to 4 significant figures on the screen.
If the numbers get large the numbers will be printed in Standard Form with a number between 1 and 10 to the required significant figures and the necessary power of 10.
print("The speed of light in a vacuum is {0:.5g} m/s to 5 sig figs.".format(299792458))
prints: The speed of light in a vacuum is 2 9979e+08 m/s to 5 sig figs
Using the % symbol formats data as a percentage
GST = 12.5 / 100
print("GST is currently set at {0:.2%}".format(GST))
Will produce GST is currently set at 12.50%