Lesson 2
Text and numbers in print
After this lesson: You'll tell text from numbers and print several things on one line.
Video
The video for this lesson is coming soon.
Explanation
You already know how to print text. Today we'll see what happens when we put a number in print - and whether quotes change anything!
Text or number?
At the school fair Bajtek runs a lemonade stand ๐, and a computer helps him count. Everything goes great... until the computer does something strange. Watch the story - click Next.
Bajtek's lemonade stand ๐ฌ
The first customers
Kuba comes to the stand. He wants 7 cups of lemonade for his team, and his sister 2 more. How many cups should Bajtek get ready? Let the computer calculate!
Remember:
- Text - anything in quotes, e.g.
"Ala","7","2 + 3". Python prints it exactly as it is - character by character. - A number - digits without quotes, e.g.
7,2026,-5. Python can calculate with numbers.
"7" and 7 look the same on the screen, but to the computer they're completely different things - like a sticker with a seven and seven real cups of lemonade.
Two cards: 7 + 3 and "7 + 3" ๐ฌ
Click Next to go step by step.
Two almost identical cards
Kuba gives Bajtek two cards: one says 7 + 3, the other "7 + 3". They look almost the same - the only difference is the quotes. But for Python they're two completely different instructions!
7 + 3, two ways
Now step through the program yourself. Line 1 has no quotes - Python calculates and prints 10. Line 2 has quotes - Python copies it character by character and prints 7 + 3.
Click the arrow and watch: what does Python calculate, and what does it just copy?
1print(7 + 3)2print("7 + 3")
โWith and without quotes
Step through the program and compare lines 1 and 2.
Click the arrow and watch: what does Python calculate, and what does it just copy?
1print("2 + 3")2print(2 + 3)3print(100)4print("100")
โLines 3 and 4 printed the same: 100. The difference shows up when we start calculating - the number 100 can be added to another number, but the text "100" can't. More about calculating in the next lesson!
The number 2026 and text with 2026
Without quotes 2026 is a number - Python can calculate with it: 2026 + 1 gives 2027. In quotes everything is text, digits too - Python copies it character by character, together with the word Text and the plus.
Click the arrow and watch: what does Python calculate, and what does it just copy?
1print(2026)2print("Text 2026")3print(2026 + 1)4print("Text 2026 + 1")
โThe robot's calculator ๐ค
Without quotes the robot calculates, in quotes it only shows the sum.
Click the arrow and watch: what does Python calculate, and what does it just copy?
1print("100 - 1")2print(100 - 1)3print("40 + 2")4print(40 + 2)
โSeveral things in one print
Bajtek has a new friend and wants to introduce himself: the screen should show one line I am 3 years old. But how do you print text and a number on one line? Watch the story - click Next to go step by step. ๐ค
Bajtek introduces himself ๐ฌ
Code
๐ฅ๏ธ Screen
How old is Bajtek?
Bajtek spent 1 year on a shelf in a robot shop, and he has lived in AlgoBaza for 2 years. Together: 1 + 2 = 3 years! Click next to see the goal of this task.
Remember Bajtek's secret: in the brackets of one print you can put several things separated by commas. Text goes in quotes, a number without quotes, and every comma turns into one space - the comma itself doesn't show on the screen.
print("I am", 3, "years old")givesI am 3 years oldprint("Ala", "Ola")givesAla Olaprint(1, 2, 3)gives1 2 3
Text and a number in one print
Now click the arrow yourself. The first line is Bajtek's introduction. Look at the labels under the pieces: orange ones are text (in quotes), blue ones are numbers (without quotes). The commas join them into one line.
Click the arrow - you'll see what print puts between the values.
1print("I am", 3, "years old")2print("Points:", 100)3print("Result:", 7 + 3)
โA comma turns into a space
Click the arrow and watch what happens to the commas between the things in print.
Click the arrow - you'll see what print puts between the values.
1print("Ala", "Ola")2print("Bajtek", 3)3print(1, 2, 3)
โCommas in print
Every comma becomes a space on the screen.
Click the arrow - you'll see what print puts between the values.
1print("Bajtek", 3)2print(1, 2, 3)3print(2, "+", 3, "=", 2 + 3)
โA sentence from pieces ๐งฉ
The first line is one whole piece of text, the second - four pieces with commas. On the screen they look the same: every comma turned into one space.
Click the arrow - you'll see what print puts between the values.
1print("Ala has 2 cats")2print("Ala", "has", 2, "cats")
โA comma inside or outside the text?
A comma inside quotes is part of the text and shows on the screen. A comma outside quotes separates things.
Click the arrow - you'll see what print puts between the values.
1print("Ala, Ola")2print("Ala", "Ola")3print("Ala,", "Ola")
โWhat will this program print?
print("1 + 1")print(1 + 1)
What does print("I am", 10, "years old") print?
Common mistakes
print(Ala)- without quotes Python looks for something called Ala and reports an error.print("5")andprint(5)look the same, but only the second can be used in calculations.
Python calculates before it prints
When print has a calculation without quotes, Python first works it out and only then prints the result.
And in quotes?
In quotes it's just text - Python calculates nothing, it copies it character by character.
Text next to a result
A comma in print joins text and a number, putting a space between them.
Click the arrow - you'll see what print puts between the values.
1print("2 + 3 =", 2 + 3)2print("I am", 7, "years old")3print("Result:", 10 + 10)
โA birthday card ๐
A comma puts a space between text and numbers.
Click the arrow - you'll see what print puts between the values.
1print("Happy birthday,", "Ola!")2print("Today you turn", 11)3print("Next year you will be", 11 + 1)
โA match scoreboard โฝ
Click the arrow - you'll see what print puts between the values.
1print("Poland", 3, ":", 1, "Germany")2print("Goals in total:", 3 + 1)
โBajtek's ID card ๐ชช
Every print has two things: text and a number (or a calculation).
Click the arrow - you'll see what print puts between the values.
1print("Name:", "Bajtek")2print("Age:", 3)3print("Legs:", 2 + 2)4print("Eyes:", 2)
โCounting apples ๐
Python calculates one step at a time, from left to right.
Shopping at the tuck shop ๐
The last line works out the total itself - no need to do it in your head.
Click the arrow - you'll see what print puts between the values.
1print("Lollipop", 2, "zl")2print("Juice", 3, "zl")3print("Roll", 1, "zl")4print("Total:", 2 + 3 + 1, "zl")
โRemember
- Text is in quotes (
"7"), a number isn't (7). - Python calculates with numbers and prints text as it is.
- A comma in print separates things and becomes a space.
Great! Now the tasks - remember: text in quotes, numbers without.
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.