AlgoBazaAlgoBaza
Python for kids ยท Module 1

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 ๐Ÿงฑ

"super" + "man"
1/2 ยท click the arrow - Python works out the next part

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.

Code
1print("foot" + "ball")
2print("Ala" + "Ola")
3print("Ala" + " " + "Ola")
Screen
โ–
1/4 ยท line 1 runs next

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

"ha" * 3
1/2 ยท click the arrow - Python works out the next part

Patterns and laughter

?

The text is in the variable now. Every print shows a different trick with text.

Code
1print("ha" * 3)
2print("=" * 7)
3print("*" * 5, "*" * 3)
Screen
โ–
1/4 ยท line 1 runs next
?

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.

Code
1print(5 + 1)
2print("5" + "1")
3print("5" + 1)
Screen
โ–
1/4 ยท line 1 runs next
?

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.

Code
1print(" " * 4 + "*" * 1)
2print(" " * 3 + "*" * 3)
3print(" " * 2 + "*" * 5)
4print(" " * 1 + "*" * 7)
5print("*" * 9)
6print(" " * 3 + "| |")
Screen
โ–
1/7 ยท line 1 runs next

One branch step by step

" " * 2 + "*" * 5
1/4 ยท click the arrow - Python works out the next part

Stairs

โ€ฆ

Click the arrow - every print draws one more line of the picture.

Code
1print("#")
2print("#" * 2)
3print("#" * 3)
4print("#" * 4)
5print("#" * 5)
Screen
โ–
1/6 ยท line 1 runs next

A chessboard โ™Ÿ๏ธ

โ€ฆ

Click the arrow - every print draws one more line of the picture.

Code
1print("#." * 4)
2print(".#" * 4)
3print("#." * 4)
4print(".#" * 4)
Screen
โ–
1/5 ยท line 1 runs next

Boom boom!

Multiplication first, then addition - just like in maths.

"bum" * 2 + "!"
1/3 ยท click the arrow - Python works out the next part

An arrow made of dashes

"=" * 5 + ">"
1/3 ยท click the arrow - Python works out the next part

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.

Code
1print("-" * 10)
2print("|" + " " * 8 + "|")
3print("|" + " " * 8 + "|")
4print("-" * 10)
Screen
โ–
1/5 ยท line 1 runs next

Remember

  • + between pieces of text joins them into one (with no extra space).
  • * text times a number repeats the text: "ha" * 3 is hahaha.
  • You can't add text and a number: "5" + 1 is 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.