Python for complete beginners · Unit 1: Getting started · Lesson 2 of 24
Lesson 2: print() and strings – talking in text
print() is the instruction you'll use more than any other. Learn exactly how it works, what strings are, and how to print text, numbers and sums.
0 of 24 lessons finished
By the end of this lesson you will be able to
- name the parts of a
printinstruction - write strings with double or single quote marks
- tell the difference between
"2 + 3"and2 + 3 - print several things on one line with commas
- join strings together with
+
The parts of a print instruction
Every print instruction has the same shape. Here are its parts:
- The name,
print. This is a function: a ready-made job that Python knows how to do. - Round brackets. When you write a function name followed by brackets, you call the function – you tell Python "do this job now".
- What goes inside the brackets. This is called the argument. It's the thing you hand to the function to work on. For print, it's what you want shown.
Think of a function as a machine with a slot. You drop something in the slot (the argument) and the machine does its job with it. The print machine's job is to show whatever you give it.
Strings: text in quote marks
A string is a piece of text. It can be a letter, a word, a sentence, or even empty. In Python you put strings inside quote marks. Both kinds work:
print("Double quotes work")
print('Single quotes work too')
It prints:
Double quotes work Single quotes work too
| Line | What it does |
|---|---|
| 1 | A string inside double quote marks. |
| 2 | A string inside single quote marks. Python treats both the same – just start and end with the same kind. |
Why have two kinds? Because sometimes the text itself contains a quote mark. If you write 'It's raining', Python sees the apostrophe in It's as the end of the string and gets confused. The fix is to wrap the string in the other kind of quote mark:
Try it Press Run. Then change the code and run it again.
print("It's raining")
print('She said "hello"')
Output
A string can hold spaces, numbers and symbols too. Python prints everything between the quote marks exactly as it is, spaces and all.
Numbers and sums
You don't have to print text. You can print numbers, and they don't need quote marks. Whole numbers like 7 or 2026 are called integers.
Python can also do math for you. The symbols + (add), - (subtract), * (multiply) and / (divide) are called operators. Look very carefully at the difference quote marks make:
print("2 + 3")
print(2 + 3)
print(10 * 4)
It prints:
2 + 3 5 40
| Line | What it does |
|---|---|
| 1 | The sum is inside quote marks, so it's a string. Python just copies the text: 2 + 3. |
| 2 | No quote marks, so Python works out the sum first and prints the answer: 5. |
| 3 | The star * means multiply. 10 × 4 is 40. |
This is one of the most important ideas in today's lesson: quote marks mean "treat this as text". Without them, Python tries to understand and work out what you wrote.
Try it Change the numbers and run it again. Try a really big sum – Python doesn't mind.
print(12 + 30)
print(100 - 1)
print(6 * 7)
Output
Printing several things at once
You can give print more than one argument. Put a comma between them, and print shows them on one line with a space in between:
print("I am", 12, "years old")
print("Total:", 5 + 5)
It prints:
I am 12 years old Total: 10
| Line | What it does |
|---|---|
| 1 | Three arguments: a string, a number and another string. Print puts a single space between each one. |
| 2 | Two arguments. Python works out 5 + 5 first, then prints Total: 10. |
Commas are handy because you can mix strings and numbers freely, and the spaces are added for you.
Joining strings with +
With two numbers, + adds. With two strings, + joins them end to end. Joining strings is called concatenation (a long word that just means "chaining together").
print("sun" + "flower")
print("sun" + " " + "flower")
It prints:
sunflower sun flower
| Line | What it does |
|---|---|
| 1 | + glues the two strings together with nothing in between: sunflower. |
| 2 | If you want a space, you have to add it yourself as a string of its own: " ". |
Unlike commas, + never adds spaces for you. And it only joins strings to strings – trying "Age: " + 12 causes an error. You'll see how to fix that in a later lesson; for now, use a comma when you want to mix text and numbers.
Try it Press Run. Then change the code and run it again.
print("rain" + "bow")
print("Hello" + ", " + "Sam")
Output
Two fun extras
print() with nothing in the brackets prints an empty line – useful for spacing out your output. And you can multiply a string by a number to repeat it:
Try it Press Run. Then change the code and run it again.
print("ha" * 3)
print()
print("-" * 20)
Output
Your turn
Remember: press Check to have your answer marked. Use Stuck? if you need a nudge.
Exercise 1 of 5 A name badge Write the code✓ Done
Write a program that prints this badge exactly, on three lines:
HELLO
my name is
Sam
The exercise opens here when you scroll to it. It needs JavaScript switched on.
Stuck? Get a nudge
Mr Babbage: Three lines of output means three print instructions.
Still stuck? Get a hint
Mr Babbage: Copy the capitals exactly: print("HELLO") first, then the other two lines.
Show me the answer
Try typing it yourself rather than copying – it sticks better.
print("HELLO")
print("my name is")
print("Sam")
Exercise 2 of 5 Text or a sum? Predict the output✓ Done
What will this program print? Type the three lines of output.
The exercise opens here when you scroll to it. It needs JavaScript switched on.
Stuck? Get a nudge
Mr Babbage: Look for the quote marks on each line. They change everything.
Still stuck? Get a hint
Mr Babbage: Text in quote marks is printed exactly as it is. + between two strings joins them with no space.
Show me the answer
Try typing it yourself rather than copying – it sticks better.
The output is:
2 + 3 5 23
Exercise 3 of 5 Fix the apostrophe Fix or change the code✓ Done
This program should print: It's my turn
Press Run to see the error, then fix it.
The exercise opens here when you scroll to it. It needs JavaScript switched on.
Stuck? Get a nudge
Mr Babbage: Python thinks the string stops at the apostrophe in It's.
Still stuck? Get a hint
Mr Babbage: Swap the single quote marks at the start and end of the string for double quote marks: "…".
Show me the answer
Try typing it yourself rather than copying – it sticks better.
print("It's my turn")
Exercise 4 of 5 Let Python do the math Write the code✓ Done
How many hours are there in a week? Write a program that prints the answer by making Python work out 7 * 24. Don't work it out yourself!
The exercise opens here when you scroll to it. It needs JavaScript switched on.
Stuck? Get a nudge
Mr Babbage: In Python, * means multiply.
Still stuck? Get a hint
Mr Babbage: Put the sum inside print with NO quote marks, so Python works it out.
Show me the answer
Try typing it yourself rather than copying – it sticks better.
print(7 * 24)
Exercise 5 of 5 Mix text and a number Fix or change the code✓ Done
Print exactly: Score: 10
Use the string "Score:" and the number 10 (not in quote marks), with a comma between them.
The exercise opens here when you scroll to it. It needs JavaScript switched on.
Stuck? Get a nudge
Mr Babbage: The program crashes because + can't join text and a number.
Still stuck? Get a hint
Mr Babbage: Replace + with a comma. The comma adds a space between the two things for you.
Show me the answer
Try typing it yourself rather than copying – it sticks better.
print("Score:", 10)
Common mistakes
| Wrong | Right | Why |
|---|---|---|
print("Hello') | print("Hello") | A string must start and end with the same kind of quote mark. |
print('It's here') | print("It's here") | The apostrophe ends the string early. Use double quote marks around text that has an apostrophe. |
print("5 * 4") | print(5 * 4) | With quote marks, Python prints the text instead of working out the answer. |
print("Total:" + 10) | print("Total:", 10) | + can't join a string and a number. Use a comma instead (for now). |
print("good"+"bye") → goodbye? | print("good", "bye") → good bye | + adds no space; a comma adds one. Choose the one that gives the output you want. |
Quick quiz
5 questions. Pick an answer for each one – if it's wrong, have another go. Finish them all to complete the lesson.
The quiz opens here when you scroll to it. It needs JavaScript switched on.
Recap
printis a function. What goes in its brackets is the argument.- A string is text in quote marks. Use
"…"or'…'; pick the other kind if the text contains one. - Without quote marks, Python works sums out:
print(2 + 3)shows 5. - Commas print several things with spaces between them.
+joins strings with no space (concatenation).
Next lesson is one of the most useful in the whole course: what to do when Python shows you an error – and why errors are completely normal.
Key words from this lesson
Tap or hover over a word for its meaning, or open the full glossary of code.
Finished?
Complete the quiz (60% or more) and this lesson is ticked off on your progress bar automatically – or tick it yourself.
About this lesson
How it's marked: exercises and quizzes are marked by Classfern in the browser – a practice check, not an exam. Your code and progress stay in this browser; we never see them.
Coach tips: Mr Babbage and Miss Lovelace pop up with tips on your first visit. Untick Coach tips at the top of the lesson (or press "All tips off" on a tip) to switch them off. Show me how always works.
Curriculum: CSTA K–12 CS Standards (grades 6–8): 2-AP-11 (variables and data), 2-AP-17 (testing and debugging), 2-AP-19 (documenting programs).
Python is a registered trademark of the Python Software Foundation. Classfern is independent and is not affiliated with or endorsed by it.
Python course home · Glossary of code · Programming Department