Chapters
Python114 chapters

Exercises

String Formatting

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

Print each row as a name padded to 12 characters, then the age right-aligned in 4.

Python
rows = [("Ada", 36), ("Grace", 45)]
for name, age in rows:
    # print the aligned row
    pass
Exercise 2

The template is a variable, so an f-string will not do. Fill it using format().

Python
template = "{} is {}"
# print Ada is 36
Exercise 3

Print the number with thousands separators and two decimals.

Python
n = 1234.5678
# print 1,234.57