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
Print a date as 30/8/2026, a time as 9:45:0 and a file path with slashes between its parts.
# 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='/')30/8/2026 9:45:0 home/admin/notes.txt
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
Ask for a number and print its table from 1 to 10, all on a single line.
# the multiplication table, on one line
num = int(input('Which table? '))
for i in range(1, 11):
print(num * i, end=' ')
print()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.
3Program 3 — a countdown that stays on one line
Count down from 5 to 1 on one line, with ... between the numbers, then Lift off! on the same line.
# a countdown that stays on one line
for n in range(5, 0, -1):
print(n, end='... ')
print('Lift off!')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
Print the items of a list separated by commas — and not apple, banana, cherry, fig, with a comma dangling at the end.
# 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])apple, banana, cherry, fig
if is testing the position, which is why this is the index form of the loop and not for f in fruits.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
Print a four-row multiplication grid, with the columns lined up.
# 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()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.
6Program 6 — a dot for every step of the work
Add the numbers 1 to 20, printing a dot for each one as it is added, then the answer on the next line.
# 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).................... 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
Dates, times, paths, comma lists. And no str() calls, because print() converts what it is given.
Tables, countdowns, dots, patterns. Something afterwards has to supply the newline, usually a bare print().
sep skips the final gap for free; end does not, so a trailing comma has to be handled with an if on the position.
9:45:0 and a sheared grid are the limit of these two. Widths and leading zeros are the next submenu.
- 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
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
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
Print the inverted star triangle using
endrather than a collector string.Hint · The outer loop counts down; the bare
print()still goes in the outer body. - 5
Print a list of marks as
72 | 65 | 88using oneprint()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.
A loop prints each value with end=', '. What is wrong with the finished line?
Why does the countdown program need no bare print() at the end?
print(9, 45, 0, sep=':') gives 9:45:0. How do you get 09:45:00?