blog / guides

python’s print() function explained: a practical beginner guide

by Luke published 8 min read

the short answer

print() shows text and values on the screen. Put what you want to show inside the brackets, like print("Hello"). Separate several values with commas, change what goes between them with sep, and change what goes at the end of the line with end.

print() shows text and values on the screen. Put what you want to show inside the brackets, and Python displays it when that line runs:

try it · python
print("Hello, world!")
Hello, world!

That is the whole idea, and it is usually the first line of Python anyone writes. This guide covers everything else you will need from print() as a beginner: printing numbers and text together, the sep and end options, f-strings, using print to find bugs, and the mistakes almost everyone makes once. Every example runs in your browser.

If you are brand new to programming, you might like to read what a computer program does first. Printing is the “give something back” job from that guide.

Text needs quotes, numbers do not

Anything inside quotes is text, which programmers call a string. Python shows it exactly as written. Numbers do not need quotes, and Python works out any sum before it prints:

try it · python
print("7 + 3")
print(7 + 3)
7 + 3
10

You can use single quotes or double quotes, as long as both ends match. 'Hello' and "Hello" print the same thing. Double quotes are handy when the text has an apostrophe in it, like "It's raining".

Printing several things at once

Separate values with commas and Python prints them on one line, with a space between each:

try it · python
name = "Priya"
score = 42
print("Player:", name, "Score:", score)
Player: Priya Score: 42

Commas are the easiest way to mix text and numbers. Python turns each value into text for you.

Joining with + is different

You can also join strings with +, but only strings. No spaces are added, and a number in the mix causes an error. There is more on that under common mistakes below.

try it · python
first = "Ada"
last = "Lovelace"
print(first + " " + last)
Ada Lovelace

Changing the gap with sep

sep is short for separator. It sets what goes between values instead of a space:

try it · python
print(2026, 11, 8, sep="-")
print("a", "b", "c", sep="")
print("eggs", "milk", "bread", sep=", ")
2026-11-8
abc
eggs, milk, bread

Changing the line ending with end

Every print() normally finishes with a new line. end lets you swap that for something else, so the next print carries on from the same line:

try it · python
print("Loading", end="")
print("...", end="")
print(" done")
Loading... done

Blank lines and many lines

An empty print() prints a blank line. To print several lines from one call, put \n where each new line should start. It stands for “new line”:

try it · python
print("Line one")
print()
print("Line three\nLine four")
Line one

Line three
Line four

Quotes and backslashes inside your text

Sooner or later you will want to print a quote mark. There are two easy ways:

  • Use the other kind of quote around the outside. print("It's sunny") works, because the apostrophe is not the kind of quote that started the text.
  • Put a backslash in front. A backslash tells Python that the next character is part of the text, not the end of it.
try it · python
print("She said \"hello\" to me.")
print('It\'s sunny.')
print("A backslash looks like this: \\")
She said "hello" to me.
It's sunny.
A backslash looks like this: \

A backslash followed by certain letters has a special meaning. \n starts a new line, as above, and \t adds a tab. That is why printing a Windows file path like C:\new can give strange output: Python reads \n as a new line. Write each backslash twice, as \\, when you mean a real backslash.

What print does with other values

print() can show any value, not just text and whole numbers. It turns each one into text first:

try it · python
print([1, 2, 3])
print({"name": "Sam"})
print(True, None)
print(3.0, 10 / 2)
[1, 2, 3]
{'name': 'Sam'}
True None
3.0 5.0

Two details catch people out. Dividing with / always gives a decimal, so 10 / 2 prints 5.0, not 5. Use // when you want a whole-number answer. And dictionaries always print with single quotes, whichever quotes you typed.

Where the output appears

Where you see the output depends on where the program runs. In a terminal it appears on the lines below the command. In a code editor like VS Code it appears in an output or terminal panel, and in IDLE, the simple editor that comes with Python, it appears in the shell window. On Gwecode it appears under the code when you press run. The output is the same everywhere. Only the window changes.

f-strings: values inside sentences

Put an f just before the opening quote, then write any variable or sum inside curly brackets. Python swaps each one for its value. This is called an f-string, and it is the neatest way to build a sentence:

try it · python
name = "Sam"
apples = 3
print(f"{name} has {apples} apples.")
print(f"Two apples each would be {apples * 2}.")
Sam has 3 apples.
Two apples each would be 6.

Rounding numbers as you print

Add a colon and a format after the value. .2f means “show two decimal places”:

try it · python
price = 7 / 3
print(price)
print(f"£{price:.2f}")
2.3333333333333335
£2.33

The long number in the first line is normal. Computers store most decimals as very close approximations, and the format tidies it up for people.

Using print to find bugs

print() is not just for showing results to people. It is also the simplest debugging tool there is. When a program gives a strange answer, print the values along the way and watch where they go wrong:

try it · python
total = 0
for n in [5, 10, 15]:
    total = total + n
    print("added", n, "total is now", total)
print("final:", total)
added 5 total is now 5
added 10 total is now 15
added 15 total is now 30
final: 30

Once you have found the problem, delete the extra prints so they do not clutter your output.

Common mistakes

Forgetting the brackets

print "Hello"

Python 3 stops with SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? This comes from old Python 2 examples online. Add the brackets.

A capital P

Print("Hello")

NameError: name 'Print' is not defined. Python cares about capital letters, and the function is print, all lower case. Recent versions add a hint suggesting print.

Mixing text and numbers with +

try it · python
age = 12
print("Age: " + age)

This stops with TypeError: can only concatenate str (not "int") to str. Use a comma, an f-string, or str(age) instead.

Quotes that do not match

print("Hello') starts with a double quote and ends with a single one, so Python never finds the end of the text. It reports a SyntaxError about an unterminated string.

Curly “smart” quotes

Code copied from a word processor or some websites can contain curly quotes, like “Hello”. Python only accepts straight quotes, so it stops with SyntaxError: invalid character. Retype the quotes in your code editor.

Trying to keep what print shows

result = print("Hi") shows Hi, but result ends up holding None, Python’s word for “nothing”. If you need the value later, store it in a variable first, then print the variable.

A short exercise

Write a program that prints a small receipt. Store three item names and three prices in variables, then print one line per item with the price to two decimal places, followed by a blank line and the total.

Aim for output like this:

Tea £1.80
Toast £2.50
Jam £0.95

Total £5.25

One possible answer

try it · python
tea = 1.80
toast = 2.50
jam = 0.95
print(f"Tea £{tea:.2f}")
print(f"Toast £{toast:.2f}")
print(f"Jam £{jam:.2f}")
print()
print(f"Total £{tea + toast + jam:.2f}")

Your next step

You have just seen most of what print() can do. The best way to make it stick is to use it in real programs. Python Level 1 on Gwecode starts with printing in its very first lessons, then moves on to variables, decisions and loops, all in 28 short lessons. It is free and runs in your browser. Create a free account to keep your progress, and start with the lesson below.

practise this · python · lessonYour first linestart the lesson

questions

Why does print need brackets?

In Python 3, print is a function, and functions are called with brackets. Very old Python 2 code wrote print "Hello" without them, which is why you still see it online. It does not work in Python 3.

How do I print without a new line?

Add end="" inside the brackets, like print("Loading", end=""). The next print then carries on from the same line.

How do I print a blank line?

Use print() with nothing inside the brackets.

What does print return?

Nothing useful. It returns None. print shows a value on screen, but it does not hand the value back, so you cannot store its result.

Can I print to a file?

Yes. Open a file for writing and pass it as file=, like print("Hello", file=f). Most of the time, though, writing to a file uses the file’s own write method.

keep going.