Output: print

Print text and numbers, several things at once.

F1.2Programming basicsGCSE10 min

Do this lesson in the simulator

print is how a program talks. You will use it in every program you ever write, to say what the robot is doing and to check what is going on inside. Showing something to the person using a program is called output.

Text goes in quotes

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("Hello from BugBot")

Run this in the simulator

The quotes tell Python where the text starts and ends. The quotes themselves are not printed. Try single quotes, 'Hello from BugBot': same result. What matters is that the opening and closing quote match.

Several things in one print

Put commas between them and Python prints them with a space in between:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("one", "two", "three")
print("the answer is", 42)

Run this in the simulator

The second line mixes text and a number. Numbers do not need quotes. 42 is a number; "42" is text that happens to look like a number. Keep that difference in mind: it matters a lot in lesson F1.8.

Printing nothing

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("above the gap")
print()
print("below the gap")

Run this in the simulator

print() with nothing inside prints an empty line. Handy for spacing out a report.

Text with quotes inside

If your text contains a quote mark, use the other kind of quote around it:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("BugBot's LED is green")
print('The robot said "hello"')

Run this in the simulator

Printing what the robot knows

You can put a question to the robot inside a print. battery() and distance() are questions; the robot answers with a number, and print shows it. Lesson F1.3 explains what the brackets mean.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("battery:", battery(), "%")
print("wall in", distance(), "cm")

Run this in the simulator

Predict, then run

Before you press Run, write down what you think each line prints. Then check.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("a", "b")
print("a" "b")
print("a,b")
print(1, 2, 3)

Run this in the simulator

The second line is a trap: two strings next to each other with no comma get glued together. Most of the time you want the comma.

Task: say hello

Print exactly three lines:

  • Hello from BugBot
  • 1 2 3 (three numbers, one print, commas between them)
  • BugBot BugBot BugBot (the word three times from one print)
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("Hello from BugBot")

Challenges

  1. Print a line with the battery and the distance both on it.
  2. Print a box made of +, - and | characters around the word BugBot, four lines tall.
  3. Find out what print("a", "b", sep="-") does.