Make the Computer Talk

Make the Computer Talk: Python for Kids lesson banner with Beep and Pixel

Track: Python for Kids  ·  Lesson 2 of 10  ·  Ages: 8+  ·  Time: 30 minutes

Last time you made a turtle draw. Today you will make Python talk. It only takes one little word to do it.

Beep says I am a robot, and I do exactly what you tell me. If you tell me to say “banana”, I will say “banana”. Even if it is a very silly thing to say. Beep boop!

What you need

  • Python and IDLE, just like in lesson 1.
  • A new file in IDLE. Choose File, then New File, and save it as talking.py.

Grown-ups In IDLE, the output of print appears in the Shell window, not in the file you are typing in. Press F5 to run the file and look at the Shell window. If a child sees a red error message, that is normal and useful: read it together, because the last line usually says what went wrong.

Your first words

The word print tells Python to show something on the screen. Type this and press F5:

print("Hello, world!")

This is what appears:

Hello, world!

The brackets ( ) hold the thing we want to show. The quote marks " " wrap around the words. A piece of text inside quote marks is called a string, because it is a string of letters, like beads on a necklace. Spaces, numbers and punctuation can be beads too.

You can use as many print lines as you like. Python runs them from top to bottom, one at a time:

print("Beep boop!")
print("I am learning Python.")
print("This is line three.")
Beep boop!
I am learning Python.
This is line three.

Pixel says Python does exactly what is written, in exactly that order. If the lines are in a different order, the words come out in a different order. Meow!

Single quotes, double quotes

You can wrap a string in double quotes "like this" or single quotes 'like this'. Both work the same. What matters is that the start and the end match.

print('Pixel says meow')
print("Pixel says meow")
Pixel says meow
Pixel says meow

Why have two kinds? Because they help when your words contain a quote mark of their own:

print("It's snack time!")
print('The robot said "hello" to the cat.')
It's snack time!
The robot said "hello" to the cat.

In the first line, the apostrophe in It's is safe inside double quotes. In the second line, the double quotes around hello are safe inside single quotes.

Beep says If you forget the closing quote, I get confused and Python shows a bug message. A bug is a mistake in a program. Every programmer makes bugs, and finding them is part of the fun.

Here is a line with a bug. The second quote mark is missing:

print("Hello)

Python will complain about this one, so always check that every opening quote has a partner.

Gluing text together

You can join strings with a plus sign +. Programmers call this joining (or concatenation, a long word for “chaining together”).

print("Hello, " + "robot!")
print("Pixel" + "Beep")
Hello, robot!
PixelBeep

Look closely at the second line. There is no space between the words, because we did not put one in. The space in "Hello, " is part of the string, so it came out in the answer. Computers never add spaces for you.

You can also give print several pieces separated by commas. This time Python adds a space between the pieces for you:

print("Beep", "and", "Pixel", "are", "friends")
Beep and Pixel are friends

And here is a strange trick. A string can be multiplied by a whole number, which repeats it:

print("ha" * 3)
print("-" * 10)
hahaha
----------

Numbers are different from strings. Compare these three lines:

print(2 + 3)
print("2 + 3")
print("2" + "3")
5
2 + 3
23

The first line is real maths, so Python adds and shows 5. The second is a string, so Python just shows it as it is. In the third, "2" and "3" are strings, so + joins them into 23.

Starting a new line

Inside a string, the two characters \n mean “start a new line”. It is like pressing Enter in the middle of the string.

print("Roses are red,\nViolets are blue,\nPython is fun,\nAnd so are you!")
Roses are red,
Violets are blue,
Python is fun,
And so are you!

Put it together: a robot greeting

print("=" * 20)
print("Beep boop! I am a robot.")
print("My favourite food is " + "batteries.")
print("I can say", "ha " * 3 + "!")
print("=" * 20)
====================
Beep boop! I am a robot.
My favourite food is batteries.
I can say ha ha ha !
====================

Can you see why there is a space before the !? The string "ha " * 3 is ha ha ha with a space at the end, and print also put a space between its two pieces.

Challenges

Try it Write a program that prints a three-line poem about your favourite animal. Use one print line for each line of the poem.

Show the answer

Your poem will be different from this one, and that is the point!

print("The cat is small and orange,")
print("She naps in the sun all day,")
print("And dreams about fish and cheese.")

Try it Make Python print a box of stars that is 3 lines tall and 12 stars wide. Use the * trick so you do not have to type all the stars.

Show a hint

A string multiplied by a number repeats. What happens if you print "" 12? You will need that line three times.

Show the answer
print("*" * 12)
print("*" * 12)
print("*" * 12)

Try it What do you think print("Go! " * 3) shows? Make a guess, then run it to check.

Show the answer
print("Go! " * 3)
Go! Go! Go! 

There is a space after the last Go!, because the string "Go! " has a space inside its quote marks.

What you learned

  • print shows things on the screen.
  • A string is text inside quote marks. You can use single or double quotes, as long as they match.
  • + joins strings together, and * with a number repeats a string.
  • A comma inside print adds a space between the pieces.
  • \n starts a new line.
  • A bug is a mistake in a program, and you can find and fix it.