Basics · lesson 2 of 10
Parts in this lesson
Aprint(a, b)several values, spaces between
Bf"{ }"values inside text
C#a note for people
D\" \nquotes and new lines
Esep= end=what goes between and after
Fprintfrom 1.1

Printing things

Last updated

Most of what a program shows you mixes fixed text with values that change. In this lesson you'll print several values at once, put values inside text with f-strings, see what happens when you forget the f, write notes in your code, and put quotes and new lines inside text.

By the end of this lesson you can

  • Print text and numbers together, on one line
  • Put values inside text with an f-string
  • Write comments, and put quotes and new lines inside text

Several values in one print

print can show more than one thing. Put commas between them, and it prints them on one line with a space between each. The values don't have to be text: numbers work too, and so does a sum, which Python works out before printing.

Press Run. Notice that you didn't type the spaces between Books on loan: and 3. print added them.

Step 1 code main.py
Edit the code, then run it again

Values inside text: f-strings

Commas are fine for simple lines, but often you want a value in the middle of a sentence. An f-string does that. Put an f right before the opening quote, then write the value inside curly braces, { and }, anywhere in the text. Python works out what's inside the braces and puts the result there.

The f stands for formatted. Press Run, then change 7 * 2 to 7 * 3 and run it again.

Step 2 code main.py
Edit the code, then run it again

Inside the braces you can write any value or sum. In lesson 1.3 you'll put names there instead, which is how f-strings are used most of the time.

Forgetting the f

This line is meant to print Due back in 14 days, but someone forgot the f. Press Run and look at what comes out.

Step 3 code main.py
Edit the code, then run it again

There was no error, and that's what makes this mistake sneaky. Python can't know you meant an f-string, so it does what the line says. When braces show up in your output, look for a missing f.

Notes for people: #

# starts a comment: a note for people reading the code. Python ignores everything from the # to the end of the line. A comment can have a line to itself, or sit at the end of a line of code.

Line 4 shows another use: putting # at the start of a line of code switches it off without deleting it. Remove the # and the space after it on line 4, then run the program again.

Step 4 code main.py
Edit the code, then run it again

Quotes and new lines inside text

A " ends the text, so how do you print a quote? There are two ways. Put a backslash in front, \", and the quote becomes part of the text (line 1). Or write the text in single quotes, and then double quotes inside it are fine (line 2). Python treats 'text' and "text" the same.

The backslash also starts other special characters. The most useful is \n, which means start a new line here (line 3). And print() with nothing in the brackets prints an empty line (line 4).

Step 5 code main.py
Edit the code, then run it again

What goes between and after: sep and end

You saw in step 1 that print puts a space between values. That space is its separator, and you can change it with sep=. Line 1 uses / instead, to print a date.

print also ends every line by starting a new one. end="" changes that to nothing, so the next print carries on on the same line (lines 2 to 4).

Step 6 code main.py
Edit the code, then run it again

sep and end are written inside the brackets, after the values, with a name and an =. They're called keyword arguments, and you'll meet more of them later.

Check what you learned

Four questions. Pick or type an answer, then check it.

What does print("Loans:", 2 + 3) print?
This program has two lines. Which one prints a value in the middle of a sentence?
print("Title:", "Dune")
print(f"Due in {7 * 2} days")
What does Python do with a line that starts with #?
Which line prints She said "hi"?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python