Lesson 7
Text: joining and repeating
After this lesson: You'll join text with a plus and repeat it with a star - and draw a tree, stairs and a frame that way.
Video
The video for this lesson is coming soon.
Explanation
You can already add and multiply numbers. Today you'll see something surprising: the + and * signs work on text too! ๐
Joining text: +
A plus between two pieces of text joins them into one - like two blocks: "foot" + "ball" is "football".
Careful: Python doesn't add a space by itself. If you want a space, you have to type it: "Ala" + " " + "Ola" is "Ala Ola".
Joining two blocks ๐งฑ
Joining in a program
The second line has no space, in the third one we typed it ourselves.
The text is in the variable now. Every print shows a different trick with text.
1print("foot" + "ball")2print("Ala" + "Ola")3print("Ala" + " " + "Ola")
โRepeating text: *
A star between text and a number repeats the text as many times as the number says: "la" * 3 is "lalala".
It's a great way to make patterns, frames and pictures - instead of typing 20 dashes, you write "-" * 20.
Repeating text
Patterns and laughter
The text is in the variable now. Every print shows a different trick with text.
1print("ha" * 3)2print("=" * 7)3print("*" * 5, "*" * 3)
โWhat does the program print?
print("3" * 4)
๐ก Fun fact: text and numbers don't mix
Two pieces of text can be joined and two numbers can be added - but you can't add text and a number: "5" + 1 is an error. ๐
Python doesn't know whether you mean 6 or "51" - so it doesn't guess at all, it stops the program. You'll learn more about errors in lesson 13.
Text plus a number = an error
Two numbers add up, two pieces of text get joined - but Python can't add text and a number. Watch what happens on the last line.
The text is in the variable now. Every print shows a different trick with text.
1print(5 + 1)2print("5" + "1")3print("5" + 1)
โWhat does the program print?
print(2 * 5, "2 * 5")
Remember the Christmas tree from lesson 1? With text multiplication we can build it much faster! ๐
A tree made by multiplying ๐
" " * 4 is four spaces, "*" * 3 is three stars. Plus joins them into one line.
Click the arrow - every print draws one more line of the picture.
1print(" " * 4 + "*" * 1)2print(" " * 3 + "*" * 3)3print(" " * 2 + "*" * 5)4print(" " * 1 + "*" * 7)5print("*" * 9)6print(" " * 3 + "| |")
โOne branch step by step
Stairs
Click the arrow - every print draws one more line of the picture.
1print("#")2print("#" * 2)3print("#" * 3)4print("#" * 4)5print("#" * 5)
โA chessboard โ๏ธ
Click the arrow - every print draws one more line of the picture.
1print("#." * 4)2print(".#" * 4)3print("#." * 4)4print(".#" * 4)
โBoom boom!
Multiplication first, then addition - just like in maths.
An arrow made of dashes
A frame made by multiplying ๐ผ๏ธ
The top and bottom are dashes times 10, and the middle is spaces times 8 between two walls.
Click the arrow - every print draws one more line of the picture.
1print("-" * 10)2print("|" + " " * 8 + "|")3print("|" + " " * 8 + "|")4print("-" * 10)
โRemember
+between pieces of text joins them into one (with no extra space).*text times a number repeats the text:"ha" * 3ishahaha.- You can't add text and a number:
"5" + 1is an error.
Well done! Now draw some pictures with text multiplication in the tasks. ๐จ
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.