Python Part I · Module 1 · Introduction to Python
print(): Making a Script Say Something
You used this line in the last chapter on trust. Now it gets taken apart. By the end of this chapter you will know what every character in it is doing — why print has brackets, why the text has quotes, what happens to the quotes themselves, and what changes when you print a number instead of a message. That level of detail is worth it exactly once, for exactly this line, because print is the single tool you will use most in the whole of Part I: it is how you find out what your program is actually doing rather than what you assumed. Once these characters are understood, later chapters can use them without stopping to explain, which is how the course stays readable as it gets harder.
Foundation10 min readPythonPart IFundamentalsprintOutput
Part I · Module 1 · Chapter 1.8 · print(): Making a Script Say Something
1. The Line, One Character at a Time
Here is the line again:
print("Hello, Python!")Six things are happening, and every one of them matters.
print — the name of something Python already provides. You did not have to write it or install it; it is built in. Its job is to display something in your terminal.
( — an opening round bracket. This is how you use something by name in Python: you write its name, then brackets, and inside the brackets you put what you want it to work on. Writing print on its own does not print anything — it names the tool without using it.
" — an opening double quote. This marks the start of a piece of text. Everything after it, until the matching closing quote, is treated as literal characters rather than as Python instructions.
Hello, Python! — the text itself. Python does not try to understand this. It is just characters, and they can be anything you like.
" — the closing double quote, marking the end of the text.
) — the closing round bracket, marking the end of what you are giving to print.
Run it and you get:
Hello, Python!2. Printing a Number
Try this one:
print(4210)4210No quotes this time — and this is a real distinction, not a style choice.
4210 without quotes is a number. Python understands it as a quantity and can do arithmetic with it.
"4210" with quotes is text that happens to contain digits. Python treats it as characters, the same as "Hello".
Both print identically, which is exactly why the difference is worth flagging now rather than when it causes a bug. Chapter 2.3 and Chapter 2.9 are about this distinction properly.
You can see it immediately at the interactive prompt from Chapter 1.5:
print(40 * 15)600Python worked out the multiplication first and gave print the answer. Put quotes round it and nothing is calculated:
print("40 * 15")40 * 153. Printing Several Things
print can take more than one thing, separated by commas:
print("seed:", 4210)seed: 4210Two things to notice:
- Python put a space between them. You did not ask for one —
printadds a single space between each item it is given. - The comma is a separator in the instruction. It does not appear in the output.
This is genuinely useful: it lets you combine a label and a value without any extra machinery. Compare:
print("Errors:", 0)
print("Test:", "axi_smoke")Errors: 0
Test: axi_smoke4. Each print Starts a New Line
Every print puts its output on its own line:
print("first")
print("second")first
secondYou never have to ask for the line break — print adds one at the end automatically. Chapter 4.2 shows how to control that when you need to, but for the whole of Module 1, one print means one line.
An empty print() gives you a blank line, which is occasionally useful for separating output:
print("Compile")
print()
print("Simulate")Compile
Simulate5. Single or Double Quotes
Both of these work and mean exactly the same thing:
print("Simulation complete")
print('Simulation complete')Python does not care which you use, as long as the closing quote matches the opening one. This course uses double quotes throughout for consistency.
The one rule: do not mix them on the same piece of text. Starting with " and ending with ' is an error, because Python is still waiting for the closing " when the line ends.
6. Common Mistakes
Forgetting a quote.
print("Simulation complete)Python reads to the end of the line still looking for the closing ", does not find one, and reports a SyntaxError. Remember from Chapter 1.4 that this stops the whole file before anything runs — even correct lines above it produce no output.
Forgetting a bracket.
print("Simulation complete"Same class of problem, same result: nothing runs.
Capitalising it.
Print("Simulation complete")Python is case-sensitive. Print and print are two different names, and only the lowercase one exists. This produces a NameError at the moment that line is reached — which, per Chapter 1.4, means any correct lines above it will have already run and printed.
Expecting the quotes to appear. They will not. That is §1.
Chapter 1.11 reads all three of these messages properly.
7. Predict, Run, Observe
Write down what you expect from each, then run them.
print("Errors:", 0)
print(4 * 5)
print("4 * 5")
print()
print('done')Five lines, and one of them produces something that looks like nothing. Check your predictions against:
Errors: 0
20
4 * 5
doneIf line 2 and line 3 surprised you, re-read §2 — that is the quoted-versus-unquoted distinction, and it is the single most useful thing in this chapter.
8. Exercise
Exercise 1 — the label habit. Write a file that prints three facts about an imaginary test run, each with a label: a name, a number, and a status. Run it. Then delete the labels so only the values print, and run it again. Which version would you rather receive from a colleague's script at 3 a.m.?
Exercise 2 — quoted or not. Predict the output of each, then check:
(a) print(100 - 1) (b) print("100 - 1") (c) print("100", "-", "1")
Explain (c) in one sentence — where did the spaces come from?
Exercise 3 — break it in three ways. Starting from a working line, make each of the three mistakes in §6 in turn. For each one, note whether any output appeared before the error. Use Chapter 1.4's rule to explain the difference between them.
Exercise 4 — blank lines. Produce this exact output, using as many print lines as you need:
Compile
Simulate
Report9. Summary
printdisplays something in your terminal. It is built into Python; nothing to install or write.- Brackets mean "use this". The name alone does nothing;
print(...)is what acts. - Quotes mark where text starts and stops and do not appear in the output. They are part of the instruction, not the message.
4210is a number;"4210"is text. They print identically, which is exactly why the difference matters later.- Commas separate several items, and
printputs a single space between them — ideal for printing a label beside a value. - Each
printstarts a new line, and an emptyprint()gives a blank one. - Single and double quotes are equivalent, as long as they match.
print is now yours, and later chapters will use it without explanation. The next chapter covers the other thing that goes in almost every file — the notes you leave for whoever reads it next.
Where this fits
Part of the Python curriculum.
