LambdaLabTM
Computer Science · Class 11 · More about print()
print()Programs⏱️ 13 min read

Programs with sep & end

Six programs where the output would be wrong, or ugly, without one of the two. None of them is difficult — the point is to recognise the moment: a punctuation mark between values means sep, and anything that must stay on one line means end.

1Program 1 — dates, times and paths

📋 The problem

Print a date as 30/8/2026, a time as 9:45:0 and a file path with slashes between its parts.

joined.py
# a date, a time and a file path, each joined by its own character

day = 30
month = 8
year = 2026

print(day, month, year, sep='/')
print(9, 45, 0, sep=':')
print('home', 'admin', 'notes.txt', sep='/')
Output
30/8/2026
9:45:0
home/admin/notes.txt
Tip
9:45:0 is honest and ugly. A real clock shows 09:45:00, and sep cannot do that — padding a number with a leading zero is a formatting job, and it is what the f-string submenu handles next. Knowing which tool stops where is half of choosing correctly.

2Program 2 — the multiplication table, on one line

📋 The problem

Ask for a number and print its table from 1 to 10, all on a single line.

table_one_line.py
# the multiplication table, on one line

num = int(input('Which table? '))

for i in range(1, 11):
    print(num * i, end=' ')

print()
Output
Which table? 7
7 14 21 28 35 42 49 56 63 70
print(num * i, end=' ')

One value, so sep never comes into it. The ending is a space instead of a newline, which is what keeps the ten answers side by side.

print()

At the margin, after the loop. Without it the shell prompt would appear at the end of the row of numbers — the row is never finished otherwise.

There is a trailing space after the 70, invisible but real, because the tenth end is added like all the others. It is harmless here; program 4 is the case where it is not.

table_one_line.py

3Program 3 — a countdown that stays on one line

📋 The problem

Count down from 5 to 1 on one line, with ... between the numbers, then Lift off! on the same line.

countdown_line.py
# a countdown that stays on one line

for n in range(5, 0, -1):
    print(n, end='... ')

print('Lift off!')
Output
5... 4... 3... 2... 1... Lift off!

No bare print() this time, and none is needed: the last print('Lift off!') has the default ending, so it finishes the line itself. An end that is not a newline hands the job of ending the line to whatever prints next.

4Program 4 — a comma-separated list, with no comma at the end

📋 The problem

Print the items of a list separated by commas — and not apple, banana, cherry, fig, with a comma dangling at the end.

comma_list.py
# the items of a list, comma separated, with no trailing comma

fruits = ['apple', 'banana', 'cherry', 'fig']

for i in range(len(fruits)):
    if i < len(fruits) - 1:
        print(fruits[i], end=', ')
    else:
        print(fruits[i])
Output
apple, banana, cherry, fig
Key Takeaway
This is the fence-post problem again. Four items have three commas between them, so the last item must be printed differently — with the ordinary ending, which also finishes the line. The if is testing the position, which is why this is the index form of the loop and not for f in fruits.
Tip
One print() can do the whole thing. print(fruits[0], fruits[1], fruits[2], fruits[3], sep=', ') gives exactly the same line, because sep puts the separator between and never at the end. That only works because there are four items and you knew it — the loop works for a list of any length.

5Program 5 — a grid of numbers

📋 The problem

Print a four-row multiplication grid, with the columns lined up.

grid.py
# a grid of numbers, printed row by row

for row in range(1, 5):
    for col in range(1, 6):
        print(row * col, end='\t')

    print()
Output
1	2	3	4	5
2	4	6	8	10
3	6	9	12	15
4	8	12	16	20

'\t' is the tab escape sequence, and it is the cheapest way to line columns up: the terminal jumps to the next tab stop rather than counting characters. The bare print() sits in the outer body, so it runs once per row — the same placement rule as the triangle on the previous page.

Watch Out
Tabs line up until they do not. Every number here is one or two digits, so the columns look neat. Add a three-digit number to a column and the tab stop moves for that row only, and the grid shears. Real column alignment needs a width, which is the f-string submenu.

6Program 6 — a dot for every step of the work

📋 The problem

Add the numbers 1 to 20, printing a dot for each one as it is added, then the answer on the next line.

dots.py
# one line of dots while something is counted

total = 0

for n in range(1, 21):
    total = total + n
    print('.', end='')

print()
print('The total is', total)
Output
....................
The total is 210

Twenty dots, one line — the shape every program that shows progress while it works. Without end it would be twenty lines each holding a single dot, which is why this is the first thing anybody wants end for after patterns.

7Recap

Punctuation between values → sep

Dates, times, paths, comma lists. And no str() calls, because print() converts what it is given.

Must stay on one line → end

Tables, countdowns, dots, patterns. Something afterwards has to supply the newline, usually a bare print().

The last one is always different

sep skips the final gap for free; end does not, so a trailing comma has to be handled with an if on the position.

Neither of them pads

9:45:0 and a sheared grid are the limit of these two. Widths and leading zeros are the next submenu.

✍️ Now write these yourself
  1. 1

    Print the numbers 1 to 20 on one line, separated by commas.

    Hint · end=', ' — and think about the last one before you decide it is finished.

  2. 2

    Print the even numbers from 2 to 20 on one line, then the odd ones on the next.

    Hint · Two loops, and a bare print() between them to end the first row.

  3. 3

    Print a word one letter at a time, on one line, with a dash between the letters.

    Hint · for ch in word: print(ch, end='-') — and the same trailing-dash question as program 4.

  4. 4

    Print the inverted star triangle using end rather than a collector string.

    Hint · The outer loop counts down; the bare print() still goes in the outer body.

  5. 5

    Print a list of marks as 72 | 65 | 88 using one print() and no loop.

    Hint · print(marks[0], marks[1], marks[2], sep=' | ') — and notice why it stops being useful for a list you did not write yourself.

Quick Check

A loop prints each value with end=', '. What is wrong with the finished line?

Quick Check

Why does the countdown program need no bare print() at the end?

Quick Check

print(9, 45, 0, sep=':') gives 9:45:0. How do you get 09:45:00?