The worksheetDownload the PDF
Answers

F3.1 String handling

Strings, lists and records · GCSE · OCR J277 2.2.3, AQA 8525 3.2.8, Edexcel 1CP2 6.3.3 · about 15 min

BugBotLab

What this lesson is about

Length, indexing, slicing, searching and joining strings, and f-strings.

Questions 11 marks in all

  1. [1 mark]What does this program print?

    first = "Bug"
    second = "Bot"
    print("Hello, " + first + second + ".")
    Answer:
    Hello, BugBot.

    + on strings joins them, and joining adds no spaces: the space is inside "Hello, ".

  2. [1 mark]What happens with print("battery: " + 92)?

    1. ATypeError: text and a number cannot be joined with +
    2. BIt prints battery: 92
    3. CIt prints battery: + 92
    4. DSyntaxError
    Answer: A. Python will not join text and a number. Use str(92) or an f-string.
  3. [1 mark]What does this program print?

    speed = 45
    print(f"driving at {speed} and half is {speed / 2}")
    Answer:
    driving at 45 and half is 22.5

    In an f-string, anything inside { } is worked out and dropped into the text.

  4. [1 mark]What does this program print?

    word = "BugBot"
    print(len(word))
    print(word.upper())
    Answer:
    6
    BUGBOT

    len counts the characters, and upper gives the text in capitals.

  5. [1 mark]What does this program print?

    print("-" * 5)
    Answer:
    -----

    Multiplying a string by a number repeats it.

  6. [1 mark]What does this program print?

    print(f"{10 / 3:.2f}")
    Answer:
    3.33

    :.2f inside the braces shows the number with two decimal places.

  7. [1 mark]What does this program print?

    word = "BugBot"
    print(word[0], word[3], word[-1])
    Answer:
    B B t

    Indexes count from 0, and -1 is the last character.

  8. [1 mark]What does this program print?

    word = "BugBot"
    print(word[1:4])
    Answer:
    ugB

    A slice starts at the first index and stops before the second: indexes 1, 2 and 3.

  9. [1 mark]What does this program print?

    message = "wall ahead"
    print(message.find("ahead"), message.find("ball"))
    Answer:
    5 -1

    find gives the index where the text starts, or -1 when it is not there.

  10. [1 mark]OCR's name.substring(3, 3) on "BugBot" gives "Bot". What does the second 3 mean?

    1. ATake 3 characters
    2. BStop at index 3
    3. CStart at index 3
    4. DRepeat 3 times
    Answer: A. In the Exam Reference Language, substring takes a start index and a number of characters. Python's slice takes a stop index instead.
  11. [1 mark]What is joining two strings together called?

    1. AConcatenation
    2. BSlicing
    3. CCasting
    4. DIteration
    Answer: A. Concatenation joins strings end to end, with + in Python.

The task: status report

Drive 10 cm, then print one line in exactly this shape (your numbers will differ):

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

forward(50, distance=10)
x, y = position()
print(f"BugBot at x={x} ...")

The hint students can ask for: Drive first, then gather the readings and print them in one line, in the order and wording the task shows.

A solution

from bugbot import *
connect()
forward(50, distance=10)
x, y = position()
print(f'BugBot at x={x} y={y}, wall in {distance()} cm, battery {battery()}%')

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.