Format an Aligned Text Table

easy
Unit 2 · Strings II0/3 to milestone

CLI tools print tables. Implement format_table(headers, rows) returning a string in exactly this format:

| name  | qty | price |
|-------|-----|-------|
| apple |   3 |  1.50 |
| kiwi  |  12 |  0.25 |

Rules:

  • A column is numeric if every non-None value in it is an int or float (but not a bool). Numeric columns are right-aligned, including their header. Other columns are left-aligned.
  • Floats are shown with 2 decimal places. None is shown as an empty cell. Everything else uses str().
  • Each column is as wide as its widest cell or header, with one space of padding on each side.
  • Lines are joined with "\n", with no trailing newline.
#strings#formatting#cli2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Format an Aligned Text Table · TechGradify