Skip to content
CCC Python Course

Output formatting

Stage
1
Module
M1.11
Lessons
1

In this module

  • Build a formatted string with an f-string, inserting a value directly inside {}.
  • Zero-pad a number and fix how many decimal places a float shows, with an f-string format spec.
  • Round a number to the nearest whole value or a chosen number of decimals with round().
  • Join a list into one line with no trailing separator, using " ".join(map(str, ...)).

Before this module

Lessons

  1. 1Output formatting

Practice

Try these on the judges. Each link opens the problem on WMOJ or DMOJ.

  1. 2021 S1
    Crazy Fencing (opens on WMOJ in a new tab) WMOJ

    Add up the areas of several trapezoid-shaped fence pieces, printing the exact total.

  2. 2018 J3
    Are we there yet? (opens on DMOJ in a new tab) DMOJ

    Print a small table of numbers as evenly spaced, space-separated rows.

    Why DMOJ: Tries the same kind of formatted output on DMOJ, which holds 2014 to 2020.