Padding Columns With padStart() and padEnd()

Before you get to regular expressions, the release notes open with the release tag and the columns that line up each ticket commit’s date, key, title, and hash. In release_notes.py, you format both with specs inside f-strings, so you port those lines first.

That turns out to be a small job with one real change. Template literals, the backtick strings that fill in ${...} the way an f-string fills in {...}, have no format specs, so writing ${MONTH:02d} in one stops JavaScript with SyntaxError: Missing } in template expression. In this lesson, you’ll move the width and fill out of the spec and into two string methods and line up the ticket column the way the Python script does.

Zero-Pad the Release Tag

The first line the script prints is the tag, built from the three date settings. In Python, :02d pads the month and day to two digits with zeros:

Language: Python
YEAR, MONTH, DAY = 2026, 11, 6

print(f"Release payments-{YEAR}.{MONTH:02d}.{DAY:02d}")

It prints Release payments-2026.11.06. The month is already two digits, so only the day gets a zero.

In JavaScript, you pad text with string methods. .padStart(width, fill) adds the fill to the front of a string until it’s width characters long. The fill is a string: "0" for the tag. Here’s the port, with each setting as its own const:

Language: JavaScript
const YEAR = 2026;
const MONTH = 11;
const DAY = 6;

const month = String(MONTH).padStart(2, "0");
const day = String(DAY).padStart(2, "0");
console.log(`Release payments-${YEAR}.${month}.${day}`);

The tag matches Python’s:

Language: Program Output
Release payments-2026.11.06

If you’ve used zfill() or rjust() in Python, .padStart() is the same idea but works only on strings. Watch out for one slip. Python’s specs pad an int as readily as a str, but DAY.padStart(2, "0") throws a TypeError because padding is for text. Turn the number into a string with String() first.

Line Up the Ticket Titles

The tag is one value. The ticket lines are a column, and the hashes line up only if every title takes the same width. The Python script pads each title on the right to 36 characters with :<36. Here are the four ticket rows, cut down to what this line needs, with dates and titles already tidied:

Language: Python
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"},
]

for row in rows:
    print(
        f"{row['date']}  {row['key']}  "
        f"{row['title']:<36}{row['sha']}"
    )

In JavaScript, {title:<36} becomes title.padEnd(36), which adds spaces to the end. With no fill argument, .padStart() and .padEnd() pad with spaces. Each row becomes an object literal, read with dot access as row.title, and for...of walks the array the way Python’s for does. The port names the width once, as a constant, and pads every row with it:

Language: JavaScript
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" },
];

for (const row of rows) {
  const title = row.title.padEnd(TITLE_WIDTH);
  console.log(`${row.date}  ${row.key}  ${title}${row.sha}`);
}

The output matches the Python script’s four lines, with every hash starting in the same column:

Language: Program Output
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

When the team wants a wider column, TITLE_WIDTH is the one place to change it.

Choose the Alignment Yourself

The Python line uses < explicitly, but plenty of format specs don’t. Without it, Python aligns numbers to the right and text to the left based on their types:

Language: Python
>>> f"{7:3}"
'  7'
>>> f"{'ab':3}"
'ab '

JavaScript can’t make that choice for you, because by the time you pad, every value is a string. So you choose the method for the alignment you want. .padStart() right-aligns, and .padEnd() left-aligns. Try both in Node’s read-eval-print loop (REPL):

Language: Node.js
> "ab".padStart(5)
'   ab'
> "ab".padEnd(5)
'ab   '

In the release notes, that’s .padStart() for the zero-padded day in the tag and .padEnd() for the title column.

Format First, Then Pad

The ticket titles are already text. An amount isn’t. PAY-2330 fixes the payout report’s totals, and that report right-aligns each amount in a 12-character column. In Python, one spec does both jobs. ,.2f turns 1303.2 into 1,303.20, and >12 right-aligns it:

Language: Python
>>> total = 1303.2
>>> f"{total:>12,.2f}"
'    1,303.20'

A template literal has no spec, so JavaScript takes two steps. A formatter makes the text, and .padStart() lines it up. The formatter is an instance of Intl.NumberFormat, JavaScript’s built-in number formatter. You build it once with new, passing a locale and options, and then call its .format() method on each amount.

The locale decides the separators and where the currency sign goes, so "en-US" gives the same grouping as Python’s ,. With style: "currency", the formatter also adds the € sign, which the Python spec leaves out. Then .padStart(12) plays the part of >12:

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

However you create the text, widths work the same way. Neither .padStart() nor .padEnd() cuts text that’s too long. "abcdef".padEnd(3) returns "abcdef", as f"{'abcdef':<3}" does. To cut plain text like these titles the way {:.3} does, call .slice(0, 3) first.

There’s no .center() either. "ab".center(6) throws a TypeError, so build centering from the two methods or choose another layout.

Widths and .slice() count UTF-16 (16-bit Unicode Transformation Format) code units, the pieces JavaScript stores text in, not the columns a terminal shows. The hashes line up here because the titles are plain ASCII. An emoji counts as two code units, and .slice() can cut one in half, so keep these methods to plain text like these titles.

So the JavaScript way is to format first, then pad. Turn each value into the text you want with String() or a formatter, name the width once, and line every row up with .padStart() or .padEnd().

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, shown as hours and minutes in a column under the ticket keys, with the week’s total underneath.

Become a Member to join the conversation.