Strings, lists and records · GCSE · OCR J277 2.2.3, AQA 8525 3.2.8, Edexcel 1CP2 6.3.3 · about 15 min
Length, indexing, slicing, searching and joining strings, and f-strings.
[1 mark]What does this program print?
first = "Bug"
second = "Bot"
print("Hello, " + first + second + ".")Hello, BugBot.
+ on strings joins them, and joining adds no spaces: the space is inside "Hello, ".
[1 mark]What happens with print("battery: " + 92)?
[1 mark]What does this program print?
speed = 45
print(f"driving at {speed} and half is {speed / 2}")driving at 45 and half is 22.5
In an f-string, anything inside { } is worked out and dropped into the text.
[1 mark]What does this program print?
word = "BugBot" print(len(word)) print(word.upper())
6 BUGBOT
len counts the characters, and upper gives the text in capitals.
[1 mark]What does this program print?
print("-" * 5)-----
Multiplying a string by a number repeats it.
[1 mark]What does this program print?
print(f"{10 / 3:.2f}")3.33
:.2f inside the braces shows the number with two decimal places.
[1 mark]What does this program print?
word = "BugBot" print(word[0], word[3], word[-1])
B B t
Indexes count from 0, and -1 is the last character.
[1 mark]What does this program print?
word = "BugBot" print(word[1:4])
ugB
A slice starts at the first index and stops before the second: indexes 1, 2 and 3.
[1 mark]What does this program print?
message = "wall ahead"
print(message.find("ahead"), message.find("ball"))5 -1
find gives the index where the text starts, or -1 when it is not there.
[1 mark]OCR's name.substring(3, 3) on "BugBot" gives "Bot". What does the second 3 mean?
[1 mark]What is joining two strings together called?
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.
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.