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:
- After an expression (something with a value, like
7 * 6), the REPL shows its value. - After an assignment like
x = 10, nothing is shown: a statement that is not an expression has no value to display. - Strings are shown with quotes:
'HELLO'. The REPL displays the repr (representation) of a value, a form meant for programmers that shows exactly what the value is.printshows the plain text.
>>> "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
7 * 6 # computed, then thrown away
print(7 * 6) # computed and written to output
Running this file prints only one 42.
print in detail
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:
sep=is placed between arguments (default" ").end=is placed after the last argument (default"\n", a newline).
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:
| Escape | Meaning |
|---|---|
\n | newline |
\t | tab |
\\ | 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
- Expecting the REPL's echo in a script. Add
print(...). - Quotes in your output.
print("'hi'")prints'hi'with quotes, because they are inside the string. If expected output has no quotes, your string should not contain them. - Stray spaces from commas.
print("Total:", 5, "!")givesTotal: 5 !. If you needTotal: 5!, usesep=""and put the spaces into the strings yourself, or use f-strings (coming in the strings module). - Smart quotes. Copying code from a word processor can bring in curly quotes, giving
SyntaxError: invalid character '"' (U+201C). Use plain ASCII quotes.
Recap
- The REPL compiles and runs one statement at a time and echoes the repr of expression values. Scripts echo nothing.
print(a, b, c, sep=..., end=...)joins arguments withsepand finishes withend, writing to stdout.- Escape sequences such as
\n,\t,\\and\"put special characters in strings; triple quotes allow multi-line strings. #starts a comment, removed before compilation.
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.