Often one wants to embed other information into strings, sometimes with special formatting constraints. In python, one may insert special formatting characters into strings that convey what type of data should be inserted and where, and how the "stringified" form should be formatted. For instance:
print('Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='115.81W'))
Or we can have the parameter values being variables:
lat = '37.24N'
lon = '115.81W'
print('Coordinates: {latitude}, {longitude}'.format(latitude=lat, longitude=lon))
We can of course re-order and use multiple times the placeholders:
lat = '37.24N'
lon = '115.81W'
print('Latitude: {latitude}, Longitude: {longitude} ==> [{latitude}, {longitude}]'
.format(latitude=lat, longitude=lon))
field ::= "{" field_name [":" format_spec] "}"
format_spec ::= [[fill character]align][sign][width][,][.precision][type]
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= number of digits in total (if width has a 0 in front, we add 0's for zero-padding)
precision ::= number of decimal points
Some common type
s:
d
integerf
floating point%
percente
exponential formatc
characters
string# Without the use of placeholders. Notice the spaces before and after the result.
print("Result: |",100/23,"|")
# Using a placeholder. Notice that the spaces around the number disappear.
print("Result: |{num}|".format(num=100/23))
# Now we specify the type of "num" and we say it will be a floating point
# Notice that we got a smaller number of decimal digits.
print("Result: |{num:f}|".format(num=100/23))
# Now let's specify the length that we want to reserve for the string
# The number above took a total of 8 characters (including the decimal)
# Now let's specify that we have 10 characters available.
# Notice the spaces in front of the number in the result
print("Result: |{num:10f}|".format(num=100/23))
# Keep six digits for the whole number, out of which 3 for the decimals
print("Result: |{num:6.3f}|".format(num=100/23))
# Compare with having 8 digits, 3 for decimals
print("Result: |{num:8.3f}|".format(num=100/23))
# Or having 8 digits, 5 for decimals
print("Result: |{num:8.5f}|".format(num=100/23))
# Keep seven digits for the whole number, out of which 2 for the decimals
print("Result: |{num:7.2f}|".format(num=100/23))
print("Result: |{num:7.2f}|".format(num=1000/23))
print("Result: |{num:7.2f}|".format(num=10000/23))
print("Result: |{num:7.2f}|".format(num=100000/23))
print("Result: |{num:7.2f}|".format(num=1000000/23))
# Sixteen digits total and four decimal digits, with comma-separated thousands
print("Result: |{num:16,.4f}|".format(num=1000000/7))
# Same example without the comma-separator
print("Result: |{num:16.4f}|".format(num=1000000/7))
# Keep six digits for the whole number, out of which 3 for the decimals, with zero padding in front
print("Result: |{num:07.3f}|".format(num=100/23))
# Floating point with three seven digits
print("Result: |{num:.7f}|".format(num=100/23))
correct = 19
total = 22
# Expressing a percentage: We use % instead of f
# We ask for 7 characters total, and two decimal numbers.
# The 7 characters include the % sign, and the decimal point
# so we have three characters allocated for the integer part
# Our number has only two digits in the integer, so we get an
# empty space in front
print('Correct answers: |{result:7.2%}|'.format(result=correct /total))
# alignment
# < means left alignment
# 30 means 30 characters allocated for the message
# s means string (it is the default, and often omitted)
print('|{message:<30s}|'.format(message='left aligned message'))
# ^ means center alignment
print('|{message:^30s}|'.format(message='centered message'))
# > means right alignment
print('|{message:>30s}|'.format(message='right aligned message'))
# fill
print('|{message:#<80s}|'.format(message='left aligned with # chars as fill'))
print('|{message:#>80s}|'.format(message='right aligned with # chars as fill'))
print('|{message:#^80s}|'.format(message='centered with # chars as fill'))
We have a list of people and scores to display.
Write code that:
We want the outcome to look like this:
Name Score
---- -----
Beth 10.0
Frederick 8.5
Panos 7.1
name1 = "Beth"
name2 = "Frederick"
name3 = "Panos"
score1 = 10.0
score2 = 8.51324
score3 = 7.12321
template_header = "{name:10s}\t{score:>7s}"
template_row = "{name:10s}\t{score:7.1f}"
print(template_header.format(name="NAME", score="SCORE"))
print(template_header.format(name="----", score="-----"))
print(template_row.format(name=name1, score=score1))
print(template_row.format(name=name2, score=score2))
print(template_row.format(name=name3, score=score3))
# Solution; do not use this before trying your own solution
name1 = "Beth"
name2 = "Frederick"
name3 = "Panos"
score1 = 10.0
score2 = 8.51324
score3 = 7.12321
# Different formatting for headers and the data rows
# since we cannot apply floating point formatting to the
# strings in the header
template_header = "{name:10s}\t{score:>7s}"
template_row = "{name:10s}\t{score:7.1f}"
# Print the header lines with the header template
print(template_header.format(name="NAME", score="SCORE"))
print(template_header.format(name="----", score="-----"))
# Print the data lines with the data template
print(template_row.format(name=name1, score=score1))
print(template_row.format(name=name2, score=score2))
print(template_row.format(name=name3, score=score3))
print("{name:10s}\t{score:7.1f}".format(name=name1, score=score1))
print("{name:10s}\t{score:7.1f}".format(name=name2, score=score2))
print("{name:10s}\t{score:7.1f}".format(name=name3, score=score3))
# Notice that we cannot use the :7.1f formatting
# for the "score" variable, when "score" is a string
# print("{name:10s}\t{score:7.1f}".format(name="NAME", score="SCORE"))
print("{name:10s}\t{score:7.1f}".format(name=name1, score=score1))
print("{name:10s}\t{score:7.1f}".format(name=name2, score=score2))
print("{name:10s}\t{score:7.1f}".format(name=name3, score=score3))
print("{name:10s}\t{score:>7s}".format(name="NAME", score="SCORE"))
print("{name:10s}\t{score:7.1f}".format(name=name1, score=score1))
print("{name:10s}\t{score:7.1f}".format(name=name2, score=score2))
print("{name:10s}\t{score:7.1f}".format(name=name3, score=score3))
print("{name:10s}\t{score:>7s}".format(name="NAME", score="SCORE"))
print("{name:10s}\t{score:>7s}".format(name="----", score="-----"))
print("{name:10s}\t{score:7.1f}".format(name=name1, score=score1))
print("{name:10s}\t{score:7.1f}".format(name=name2, score=score2))
print("{name:10s}\t{score:7.1f}".format(name=name3, score=score3))
template_header = "{name:10s}\t{score:>7s}"
template_row = "{name:10s}\t{score:7.1f}"
print(template_header.format(name="NAME", score="SCORE"))
print(template_header.format(name="----", score="-----"))
print(template_row.format(name=name1, score=score1))
print(template_row.format(name=name2, score=score2))
print(template_row.format(name=name3, score=score3))