Foundations · about 30 min

The REPL, scripts, print and comments

Why this matters

You will spend your Python life switching between two ways of running code: typing lines interactively to try things out, and writing scripts that run the same way every time. Knowing what each does differently (especially about output) avoids a classic beginner confusion: "it showed the answer in the REPL, why does my script print nothing?" This lesson also covers print properly, since it is how your programs talk to the world and how learnprog checks your work.

The REPL

Run python3 with no arguments and you get the interactive prompt:

$ python3
Python 3.13.5 (main, ...) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

This is the REPL: Read a line, Evaluate it, Print the result, Loop. Each time you press Enter, CPython compiles what you typed into bytecode and runs it immediately, using the same machinery as for a file, just one statement at a time.

>>> 7 * 6
42
>>> "hello".upper()
'HELLO'
>>> x = 10
>>> x + 5
15

Notice three things:

>>> "tab\there"
'tab\there'
>>> print("tab\there")
tab    here

Leave the REPL with exit() or Ctrl-D (Ctrl-Z then Enter on Windows). The prompt ... appears when Python is waiting for you to finish a multi-line statement; you will see it once you write blocks with if and for.

The REPL is a laboratory. Whenever you wonder "what does this do?", try it there. Use help(print) for documentation on any function.

Scripts

A script is a file of Python code run as a whole: python3 main.py. Every learnprog exercise is a script. The crucial difference:

Note: A script never echoes values automatically. If a line computes a value and you do not print it or store it, the value is discarded.

7 * 6          # computed, then thrown away
print(7 * 6)   # computed and written to output

Running this file prints only one 42.

print takes any number of arguments, separated by commas, converts each to text, and writes them separated by a space, followed by a newline:

print("Total:", 3 + 4, "items")
Total: 7 items

Two keyword arguments change the separators. You give them by name, after the ordinary arguments:

print("a", "b", "c")
print("a", "b", "c", sep="")
print("a", "b", "c", sep=", ")
print("no newline", end="")
print(" ... continued")
a b c
abc
a, b, c
no newline ... continued

print() with no arguments prints an empty line.

Where print writes: stdout

Every process on Linux starts with three open streams: standard input (stdin, file descriptor 0), standard output (stdout, 1) and standard error (stderr, 2). print writes to stdout. In a terminal, stdout is shown on screen; but it can be redirected to a file (python3 main.py > out.txt) or piped to another program. learnprog captures your program's stdout and compares it with the expected output. Error messages and tracebacks go to stderr, which is why they are shown separately.

Strings and escape sequences

A string literal can use single or double quotes; they mean the same thing. Pick the one that avoids clashing with the text:

print('She said "hi"')
print("It's fine")

Inside a string, a backslash starts an escape sequence, a way to write characters that are hard to type or would end the string:

EscapeMeaning
\nnewline
\ttab
\\a single backslash
\"double quote (inside "...")
\'single quote (inside '...')
print("line one\nline two")
print("C:\\Users\\ada")
line one
line two
C:\Users\ada

Triple-quoted strings ("""...""" or '''...''') can span several lines; the line breaks become part of the text:

print("""Roses are red,
Violets are blue.""")

Comments

A # starts a comment: Python ignores everything from # to the end of the line. The tokenizer drops comments, so they never reach the bytecode and cost nothing at run time.

# Compute the area of a 3 by 4 rectangle.
width = 3
height = 4
print(width * height)  # 12

Good comments explain why, not what: # 4 because the API pages in groups of four is useful; # set x to 4 is noise. Commenting a line out is also a quick way to disable it while experimenting, as one exercise asks you to do.

Common mistakes

Recap

Going further: in the REPL, type help(print) and read the full signature. Also try python3 -c 'print("hi")', which runs a one-line program given on the command line.

Exercises

Exercise 1 · quiz

Echo in the REPL

You type the expression 7 * 6 on a line by itself. In which situation does 42 appear on screen without any print?

Answer
  • In the interactive REPL

The REPL (Read-Eval-Print Loop) prints the value of each expression you type, as a convenience (using repr). A script does no such thing: an expression statement on its own computes a value and throws it away. In scripts, output only happens when you ask for it with print.

Exercise 2 · write code

Controlling separators

Using exactly two print calls, produce this output:

2026-11-01 -> done

The first print must take the three values "2026", "11", "01" as separate arguments and use sep and end so that the second print("done") continues on the same line. (The output is one line ending in a newline.)

File: main.py

Starting point

print("2026", "11", "01")
print("done")

Example

expected output

2026-11-01 -> done
Hint 1

sep is what print puts between arguments (default: one space).

Hint 2

end is what print puts after the last argument (default: a newline). Set it to " -> ".

Reference solution
print("2026", "11", "01", sep="-", end=" -> ")
print("done")

Exercise 3 · write code

Escape sequences

With a single print call, print this exact output (three lines; the second line starts with a tab character, and the third contains double quotes):

Name:
    Ada Lovelace
She said "hello".

Use the escape sequences \n (newline) and \t (tab) inside one string.

File: main.py

Starting point

print("Name:")

Example

expected output

Name:
	Ada Lovelace
She said "hello".
Hint 1

Inside a string, \n starts a new line and \t inserts a tab.

Hint 2

To put a double quote inside a double-quoted string, write \", or use single quotes around the whole string.

Reference solution
print("Name:\n\tAda Lovelace\nShe said \"hello\".")

Exercise 4 · write code

Comments

The starter program prints four lines. Without deleting any line, turn the two lines that print DEBUG messages into comments, so that the output is exactly:

Starting
Finished

File: main.py

Starting point

print("Starting")
print("DEBUG: x is 3")
print("DEBUG: about to finish")
print("Finished")

Example

expected output

Starting
Finished
Hint 1

A # makes Python ignore the rest of the line.

Reference solution
print("Starting")
# print("DEBUG: x is 3")
# print("DEBUG: about to finish")
print("Finished")