Padding Columns With padStart() and padEnd()

The release notes open with the tag and the ticket columns, which release_notes.py formats with f-string specs. Template literals, the backtick strings that fill in ${...} the way an f-string fills in {...}, have no format specs: ${MONTH:02d} stops JavaScript with SyntaxError: Missing } in template expression. The width and fill move out of the spec and into two string methods.

Move the Width Into a Method

Each spec part the script uses maps onto a method. The padding methods take an optional fill string as their second argument and use spaces by default:

Python JavaScript
f"{DAY:02d}", str(DAY).zfill(2) String(DAY).padStart(2, "0")
f"{title:<36}", title.ljust(36) title.padEnd(36)
f"{title:.36}" title.slice(0, 36)

Here are the tag and the four ticket rows, cut down to the fields you need, with the width named once as a constant. Each row is an object literal whose properties you access with dot notation, as in row.title. The for...of loop walks the array as Python’s for does:

Language: JavaScript
const YEAR = 2026;
const MONTH = 11;
const DAY = 6;
const TITLE_WIDTH = 36;
const rows = [
  { date: "06/11", key: "PAY-2330",
    title: "Payout report totals off by a cent", sha: "0d9a6c3" },
  { date: "04/11", key: "PAY-2317",
    title: "Add the SEPA mandate to receipts", sha: "c2d4a61" },
  { date: "03/11", key: "PAY-2304",
    title: "Retry failed card payments", sha: "3ab08d2" },
  { date: "02/11", key: "PAY-2291",
    title: "Show refunds in their own currency", sha: "9c1e4b7" },
];

const month = String(MONTH).padStart(2, "0");
const day = String(DAY).padStart(2, "0");
console.log(`Release payments-${YEAR}.${month}.${day}`);
for (const row of rows) {
  const title = row.title.padEnd(TITLE_WIDTH);
  console.log(`${row.date}  ${row.key}  ${title}${row.sha}`);
}

Run it, and the columns line up:

Language: Program Output
Release payments-2026.11.06
06/11  PAY-2330  Payout report totals off by a cent  0d9a6c3
04/11  PAY-2317  Add the SEPA mandate to receipts    c2d4a61
03/11  PAY-2304  Retry failed card payments          3ab08d2
02/11  PAY-2291  Show refunds in their own currency  9c1e4b7

The code pads every title to the same TITLE_WIDTH of 36 characters, so each hash starts in the same column.

Watch the Spots Where Specs and Methods Differ

A spec works on both numbers and strings. The methods take strings only. Keep these four cases in mind:

  • Padding a number: DAY.padStart(2, "0") throws TypeError: DAY.padStart is not a function. Turn the number into text first with String(DAY). padStart() behaves differently from zfill() for negative numbers. Python keeps the minus sign in front ('-05'), while String(-5).padStart(3, "0") gives "0-5".
  • Default alignment: Python right-aligns numbers and left-aligns text, so f"{7:3}" is ' 7' and f"{'ab':3}" is 'ab '. By the time you pad, every value is a string, so you choose the alignment yourself. .padStart() right-aligns and .padEnd() left-aligns.
  • Long text: Neither method truncates text, and neither do < and >. "abcdef".padEnd(3) is "abcdef". To truncate text as {:.3} does, call .slice(0, 3) first.
  • Centering: There’s no .center(). "ab".center(6) throws a TypeError, so combine the two padding methods to center the text or choose another layout.

The padding methods and .slice() count UTF-16 (16-bit Unicode Transformation Format) code units, not characters or terminal columns. That makes a difference only beyond printable ASCII, where an emoji takes two units and .slice() can split it in half. That’s a topic for later.

Format the Value Before You Pad It

PAY-2330 fixes the payout report, which right-aligns each amount in a 12-character column. Python does it in one spec, f"{1303.2:>12,.2f}", giving ' 1,303.20'. In JavaScript, an Intl.NumberFormat instance formats the amount, and .padStart() lines it up:

Language: Node.js
> const opts = { style: "currency", currency: "EUR" };
undefined
> const euros = new Intl.NumberFormat("en-US", opts);
undefined
> euros.format(1303.2).padStart(12)
'   €1,303.20'

The formatter also adds the € sign, which Python’s spec leaves out, and .padStart(12) plays the part of >12.

That’s the JavaScript way for every column: turn each value into the text you want with String(), .toFixed(), or a formatter. Name the width once as a constant, and pad every row with .padStart() for right alignment and zero padding or .padEnd() for a left-aligned column.

In the next exercise, Total the Week’s Time Sheet, you’ll port the time sheet the lead posts with the release notes: each ticket’s minutes logged, printed as hours and minutes in a column under the ticket keys, with the week’s total underneath.

Become a Member to join the conversation.