Container Data Structures
String Formatting
Strings can be concatenated with each other as in :
int_course_header = "Introduction to Programming"
adv_course_header = "Advanced Programming"
language_list = ["C/C++","Java","Python"]
for language in language_list:
print(int_course_header + " with " + language)
However, strings cannot be concatenated with numbers directly:
course_header = "Introduction to Programming with Python"
version_list = [2, 3]
for version in version_list:
print(course_header + version)
#print(course_header + str(version))
It is possible to format the string as a template and fill in the gaps {}
later with numbers:
course_header = "Introduction to Programming with Python{}"
version_list = [2, 3]
for version in version_list:
print(course_header.format(version))
Formatting can be applied to multiple placeholders {}
in the strings:
course_header = "Introduction to Programming with Python{} for {} {}"
version_list = [2, 3]
for semester in ["Spring", "Summer", "Fall"]:
for year in range(2021,2025):
for version in version_list:
print(course_header.format(version, semester, year))
Additionally, the order of placeholders can be specified manually, if necessary:
course_header = "Introduction to Programming with Python{0} for {2} {1}"
version_list = [2, 3]
for semester in ["Spring", "Summer", "Fall"]:
for year in range(2021,2025):
for version in version_list:
print(course_header.format(version, semester, year))
The following formatting will also produce a similar output with the format operator "%"
:
course_header = "Introduction to Programming with Python %d for %s %d"
version_list = [2, 3]
for semester in ["Spring", "Summer", "Fall"]:
for year in range(2021,2025):
for version in version_list:
print(course_header % (version, semester, year))
where %d
is used as an integer numeric placeholder (truncating decimal part of floats) and %s
is used for a text placeholder.
%f
can be used for floating point numbers where we can specify the length of the decimal part as in
account_balance = "Balance: %f"
account_balance_2 = "Balance: %.2f"
account_balance_3 = "Balance: %.3f"
account_balance_10_2= "Balance: %10.2f"
balance = 145.6753424
print(account_balance % balance)
print(account_balance_2 % balance)
print(account_balance_3 % balance)
print(account_balance_10_2 % balance)
print()
balance_list=[1.2356, 12.67899, 2676.67544, 45.4538, 67785.4334]
total = 0
for bal in balance_list:
total += bal
print(account_balance_10_2 % bal)
print("Total:%13.2f" % total)
print()
balance_dict = {"Harry Potter":345.3456, "Frodo":5642.44432, "Batman":1334566.4332}
for key in balance_dict.keys():
print("Balance of %15s is %10.2f" % (key, balance_dict[key]))
where %10.2f
means that python will reserve a total length of 10 characters for the output, leaving spaces before printing the number if necessary. This is useful in printing values to console or paper in a structured format where you would need your numbers to be aligned correctly in the same column.
Output:
Balance: 145.675342
Balance: 145.68
Balance: 145.675
Balance: 145.68
Balance: 1.24
Balance: 12.68
Balance: 2676.68
Balance: 45.45
Balance: 67785.43
Total: 70521.48
Balance of Harry Potter is 345.35
Balance of Frodo is 5642.44
Balance of Batman is 1334566.43
Try aligning the numbers to the left of the reserved area and leaving the necessary spaces afterwards. Please share the solution on moodle or in class groups.