Python lesson 1 of 9
Python Printing and Output
Learn what print really does in Python - quotes and strings, printing values with commas, the sep and end settings, escapes and multi-line text.
Published · Every example on this page was run before it was published.
A program is silent by default. It can add up a thousand numbers, sort a list of names, and work out the exact cost of your shopping, and unless you tell it to say something you will see nothing at all. That is not a fault. A computer has no opinion about which of its internal results you care about, so it shows you none of them until you point at one and ask.
print is how you ask. It is the first thing almost everybody learns, and it stays useful forever. It
is how a finished program reports its results, and it is how you look inside a program that is not
doing what you expected. Long after you have learned loops and functions, print is still the tool
you reach for when something is wrong and you need to see what is actually happening.
This lesson is about that one function, and about the quotes, commas and escape characters that surround it. None of it assumes you have written any code before. By the end you will be able to put text on the screen exactly where and how you want it, including multi-line blocks and tidy columns built from nothing but ordinary print calls.
What print Actually Does
print is a function — a piece of code that already exists inside Python, ready for you to use.
You use a function by writing its name followed by a pair of round brackets, and whatever you put
between those brackets is handed over for the function to work with. Values handed over this way are
called arguments.
So print("Coffee is ready") gives one argument, the text Coffee is ready, to print, and print
writes it to the screen.
print("Coffee is ready")Coffee is readyNothing else happens. Python read the line, did what it said, and moved on. That is worth stating plainly, because the opposite case surprises people: a line that calculates something but never prints it produces no output whatsoever.
5 + 3
"a line of text sitting on its own"
print(5 + 3)8The first line added 5 and 3. Python genuinely did that addition — it just had nowhere to put the
answer, so the answer was thrown away. The second line is a piece of text that nobody asked to see.
Only the third line, the one wrapped in print, reaches the screen. When you run a program and see
nothing, this is almost always why: the work happened, and nobody asked for the result.
Each print finishes the line it wrote, which is why the next one begins underneath rather than
alongside, and several print calls stack up in the order you wrote them.
print("Kettle on")
print("Water boiling")
print("Tea poured")Kettle on
Water boiling
Tea pouredThree lines of code, three lines of output, top to bottom. Python does not reorder anything and does not tidy anything up.
Quotes Turn Words Into Text
Look again at what went inside the brackets: "Coffee is ready", with a quotation mark at each end.
Those quotes are not decoration. They are how you tell Python that the characters between them are a
piece of text to be taken literally, rather than the name of something in your program. Text handled
this way is called a string, because it is a run of characters threaded together in order.
Python accepts single quotes and double quotes and treats them identically. Pick whichever you prefer and stay consistent.
print('Single quotes are fine')
print("So are double quotes")Single quotes are fine
So are double quotesWhat matters is that the quote at the end matches the quote at the start.
Take the quotes away and the meaning changes completely:
print(Hello)NameError: name 'Hello' is not definedWithout quotes, Hello is no longer text — it is a name, and Python goes looking for something in
your program called Hello. There is nothing by that name, so it stops and says so. A NameError
always means the same thing: you referred to something Python has never been introduced to. When the
thing you meant was a piece of text, the fix is quotes around it.
The real message arrives with a couple of extra lines above it showing which line of your program failed and what it said. The last line is the one that names the problem, and it is the part to read first.
Putting a Quote Inside a String
An apostrophe is a single quote character, which creates an obvious problem if single quotes are also what marks the start and end of your string. There are two clean ways out and you will use both.
print("It is nearly five o'clock")
print('She said "good morning" and walked on')
print('It is nearly five o\'clock')It is nearly five o'clock
She said "good morning" and walked on
It is nearly five o'clockThe first line wraps the text in double quotes, so the apostrophe inside is just another character. The second does the reverse: single quotes on the outside leave double quotes free to appear inside. This is the practical reason Python offers both styles rather than one.
The third line shows the other approach. A backslash immediately before a quote tells Python that this particular quote belongs to the text and does not end the string. A backslash used this way is called an escape character, because it changes the meaning of the character that follows it. Switching to the other quote style is usually easier to read, but the backslash always works — including when both kinds of quote appear in the same sentence.
Printing Several Values at Once
print accepts more than one argument. Separate them with commas and they all land on the same line.
print("Guests:", 23)
print("red", "green", "blue")Guests: 23
red green blueNotice what Python did between them: it inserted a single space. You did not type that space, and it
appears between every adjacent pair of values. This is a deliberate convenience, and it is why
print("Guests:", 23) reads correctly without you having to think about spacing at all.
It also means that typing your own space gets you two.
print("Total:", 99)
print("Total: ", 99)Total: 99
Total: 99The second line has a space you typed inside the quotes and a space Python added, one after the other. When a gap in your output looks slightly too wide, this is usually the cause.
Choosing What Goes Between: sep
That automatic single space is a default, not a rule. print takes an extra setting called sep,
short for separator, which replaces whatever goes between the values. You write it after the values,
as sep= followed by the string you want.
print("2026", "09", "11", sep="-")
print("C", "H", "L", sep="")
print("tea", "coffee", "juice", sep=" | ")2026-09-11
CHL
tea | coffee | juiceThree useful shapes. A single character joins values into a formatted date. An empty string — written as two quote marks with nothing between them — glues the values together with no gap at all. And a longer string lets you build something readable out of separate pieces.
sep only affects the gaps between values, so it does nothing at all when you give print a single
value, because then there are no gaps to fill.
Choosing What Comes After: end
By default, print finishes by moving to a new line. That is why consecutive print calls stack up
vertically instead of running together. This too is a setting, called end, and it holds whatever
gets added after the last value.
Set end to an empty string and the next print carries straight on from where the previous one
stopped.
print("Loading", end="")
print("...", end="")
print("done")Loading...doneThree print calls, one line of output. The first two ended with nothing, so each following call
continued the same line. The third used the default ending, which is what finally closed the line.
end can also be longer than the default. And print() with no arguments at all prints only its
ending, which amounts to a blank line.
print("Chapter one")
print()
print("Chapter two")
print("The end", end="\n\n")
print("Credits")Chapter one
Chapter two
The end
Creditsprint() on its own is the shortest way to space output out. The end="\n\n" on the fourth line says
"finish this line and then leave a blank one", doing the same job without a separate call.
Together, sep and end cover most of the small formatting frustrations beginners run into. They are
unusual among Python's features in that they are simple, immediately useful, and almost never
mentioned until much later than they should be.
Escape Sequences
The \n in that last example is a newline character — the invisible character that ends a line.
You cannot type it directly inside an ordinary quoted string, because pressing Enter would end your
line of code instead. So you write a backslash followed by n, and Python translates the pair into
the single character it stands for. A backslash pairing like this is called an escape sequence.
Three of them are worth learning straight away.
print("Line one\nLine two")
print("Name\tScore")
print("Ada\t91")
print("Saved to C:\\reports\\july.txt")Line one
Line two
Name Score
Ada 91
Saved to C:\reports\july.txt\n starts a new line in the middle of a string, which means one print can produce several lines of
output. \t is a tab, which jumps ahead to the next tab stop and is the quickest way to get two
columns roughly lining up. And \\, two backslashes, produces one literal backslash — which you need
whenever a backslash is genuinely part of your text, as in a Windows file path.
That last one exists because the backslash has been given a job. Once a character means "change the meaning of the next character", it needs an escape sequence of its own in order to mean itself again.
Several Lines in One String
Escaping every line break with \n gets unreadable once there are more than two of them. For a block
of text, use a triple-quoted string: three quote marks to open and three to close. Everything
between them is kept exactly as typed, line breaks and leading spaces included.
print("""Corner Cafe
17 Mill Lane
Open until six""")
print("---")
print("""
Corner Cafe""")Corner Cafe
17 Mill Lane
Open until six
---
Corner CafeThe indentation on the middle two lines is part of the string rather than part of your program's structure, so it comes out untouched.
The --- line separates the two halves of that example, and the second half shows the one thing that
catches everyone. If you press Enter immediately after the opening """, that line break is part of
the string, so your output begins with a blank line. To avoid it, start your text on the same line as
the opening quotes.
Numbers and Text Together
Numbers do not need quotes. 23 written without quotes is the number twenty-three, and Python can do
arithmetic with it. Hand it to print alongside some text and both come out fine.
print("Guests:", 23)
print("Slices needed:", 23 * 3)Guests: 23
Slices needed: 69* is Python's multiplication sign, so 23 * 3 is sixty-nine. Python worked that out before printing
anything, because an argument can be a calculation and Python always works out the value before
handing it over.
Now for the trap. Text can be joined with +, and numbers can be added with +, so it looks as
though + ought to be able to stick a number onto the end of a sentence. It cannot.
print("Guests: " + 23)TypeError: can only concatenate str (not "int") to strPython refuses to guess. A + between two strings joins them, a + between two numbers adds them,
and a + between one of each has no single sensible meaning, so Python raises a TypeError — the
error you get when a value is the wrong kind for the operation being attempted. str is Python's name
for a string and int its name for a whole number, so read the message as: the only thing you may
join onto a string is another string.
Two fixes are available, and one of them you already know.
print("Guests: " + str(23))
print("Guests:", 23)Guests: 23
Guests: 23str(23) builds the text "23" out of the number, which + is then perfectly happy to join. But the
comma version is shorter and there is nothing to forget, which is why commas are the right default
while you are starting out. Reach for + when you need two pieces of text joined with no space
between them, and use commas the rest of the time.
There is a third way, which you will see everywhere in real Python code: an f-string, written in
the shape f"Slices: {23 * 3}", which drops values straight into a sentence at the points you mark
with curly braces. It is genuinely the nicest of the three, and it is taught properly in the
strings lesson. Commas will carry you a long way in the meantime, and meeting
them first means you will understand exactly what f-strings are saving you from.
A Worked Example
Everything above is enough to build something with real visible structure. Here is a cafe receipt,
produced entirely by print — no library, no formatting system, nothing you have not met on this page.
print("+----------------------------+")
print("| CORNER CAFE |")
print("+----------------------------+")
print("Flat white", "3.20", sep=" .............. ")
print("Almond croissant", "2.75", sep=" ........ ")
print("Sparkling water", "1.50", sep=" ......... ")
print("+----------------------------+")
print("Items:", 3, end=" ")
print("Total:", 7.45)
print("+----------------------------+")
print("""
Thank you. Come again.""")+----------------------------+
| CORNER CAFE |
+----------------------------+
Flat white .............. 3.20
Almond croissant ........ 2.75
Sparkling water ......... 1.50
+----------------------------+
Items: 3 Total: 7.45
+----------------------------+
Thank you. Come again.Reading it from the top. The + and - characters form the border. They are ordinary text inside
quotes, and Python has no idea they are meant to look like a line — the shape exists only in your eye.
The width was chosen once, at thirty characters, and every bordered line is padded with spaces to
match it, which is what puts CORNER CAFE in the middle.
The three item rows are where sep does real work. Each row hands print exactly two values, the
item and its price, and sep supplies the run of dots that sits between them. The dot counts differ
from row to row because the item names differ in length, and each run was chosen so that the finished
row comes to the same thirty characters as the borders. Counting dots by hand does not scale, and
Python has proper alignment tools for when it matters, but doing it this way makes it obvious that the
dots are a separator and not part of either value.
The totals line shows end doing the same kind of job in the other direction. The first print ends
with six spaces instead of a line break, so the second print continues on the same line. Two
separate calls, one line of output — and the reason to split them at all is that each half is a label
and a value that commas handle neatly.
The footer is a triple-quoted string that deliberately begins with a line break, which is what puts the blank line above the thank-you. That is the same behaviour flagged as a gotcha earlier. Used on purpose, it is simply a blank line.
Nothing in this program is clever. It is the plainest possible use of the tools on this page, and it still produces something that looks deliberate. That gap, between simple tools and structured output, is most of what early programming turns out to be.
Common Mistakes
Capitalising the name
Print("Corner Cafe")NameError: name 'Print' is not defined. Did you mean: 'print'?Python is case-sensitive, which means Print, PRINT and print are three different names and
only the lowercase one exists. Recent versions of Python guess what you meant and say so on the same
line, which makes this a quick fix — though read the suggestion rather than trusting it blindly,
because it is a guess based on spelling.
Leaving a quote or a bracket unclosed
print("Corner Cafe)SyntaxError: unterminated string literal (detected at line 1)The closing quote is missing, so Python runs off the end of the line while still inside the string and gives up. A missing bracket has the same cause and a different message:
print("Corner Cafe"SyntaxError: '(' was never closedA SyntaxError is different in kind from the other errors on this page. It means Python could not
read your program at all, so nothing ran — not even the correct lines above the broken one. Quotes and
brackets always come in pairs, and the fastest habit for avoiding this is to type both halves of a
pair first and then fill in what goes between them.
Expecting + to leave a space
print("Corner" + "Cafe")
print("Corner", "Cafe")CornerCafe
Corner Cafe+ joins two strings with nothing in between, exactly as written. The automatic single space belongs
to the comma, not to string joining. If you want a space with +, you have to include it yourself,
inside one of the strings or as a third piece.
An unescaped backslash in a path
print("C:\new\table.txt")
print("C:\\new\\table.txt")C:
ew able.txt
C:\new\table.txtThe first line was meant to be a file path. Instead \n became a newline and \t became a tab,
because that is precisely what those pairs mean inside a string — the backslashes were doing their
escaping job, silently and correctly. No error was raised, which makes this harder to catch than a
crash. Double every backslash you intend to be a literal backslash.
Expecting a line to show itself
"Corner Cafe"
23 * 3
print("Corner Cafe", 23 * 3)Corner Cafe 69Three lines of code, one line of output. The first two are perfectly valid Python: a string, and a
multiplication Python really performed. Neither goes anywhere, because neither was handed to print.
This is easy to do by accident when you delete a print while experimenting and leave its contents
sitting on the line. If a value should appear, it has to be inside the brackets.
Next Steps
The linked practice problem, reverse-words-in-a-string, is a clear step up from anything on this
page, and that is deliberate. Every attempt you make at it will be checked by printing the result, and
every time the result comes out wrong, print is how you find out which part of the string went
astray. Printing is not a beginner topic you leave behind — it is the instrument you debug with for
the rest of your programming life.
Before that, spend ten minutes in the Python playground. Print a line, then
print it again with sep set to something odd. Put a \t in the middle of a sentence and see where
it lands. Delete a closing quote on purpose, so that you recognise the SyntaxError on sight when it
turns up for real. Then open the first exercise,
Hello, Out Loud. It hands you a finished banner and asks for one
change — your own name in place of the placeholder, quotes left exactly where they are. Nothing on
this page has to be written from scratch to finish it.
Write it yourself
Reading about code and writing it are different skills. These exercises practise exactly what this lesson covered; they run in this tab and need no account.
When you want a harder one
Interview-style problems graded against hidden tests — a big step up from the exercises. Come back to these when the ideas in this lesson feel comfortable rather than new.