Python Programming · Middle Stage (6–8) · 1 course, 13 worked problems, 57 practice questions
A string is a piece of text written between quotes, such as name = "Asha". Each character has an index that starts at 0, so name[0] is "A". A slice such as name[1:3] gives part of the string. Strings can be joined with + and repeated with *, and len() gives their length. Methods such as .upper(), .lower(), .strip() and .replace() return changed copies, because a string itself cannot be changed. An f-string puts values into text, and a for loop can visit each character in turn.
.upper() and .replace()Choose an answer, then check it. Each one shows a full explanation.
A program takes two characters and a slice from a word. s = "certify" print(s[2], s[-2], s[1:4]) What does the program print?
A student wants to change the word "data" so that it starts with a capital D. The program starts with s = "data", and then one of four lines is run.
s = "data"
Which line runs with no error?
A loop cuts a string into pieces of two characters and builds a new string from the pieces. s = "abcdef" t = "" for i in range(0, len(s), 2): t = s[i:i + 2] + t print(t) What does the program print?
Each one has a hint, the key idea and a full worked solution.
Five claims about the word loop use the in operator, slices that start inside or past the end, and the ordering of text.
Pick the claims that hold. More than one may be right.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
A function builds the initials of a name, which has two spaces after its first word. Then the program splits a text on one space. def initials(name): out = "" for word in name.split(): out += word[0].upper() return out print(initials("ada king lovelace"), len("ada king".split(" "))) What happens when the program runs?
A method of a class calls a string method on an attribute. class Label: def __init__(self, text): self.text = text def hide(self): self.text.replace("o", "0") return self.text + "!" a = Label("door") print(a.hide(), a.text) What does the program print?
A class keeps a code with its spaces removed, and a method picks out its digits. class Code: def __init__(self, raw): self.raw = raw.strip() def digits(self): return "".join(ch for ch in self.raw if ch.isdigit()) c = Code(" A-12-b7 ") print(c.digits(), int(c.digits()) + 1, c.raw.split("-")) What does the program print?
A program finds the dots in a file name. path = "data.backup.tar.gz" first = path.find(".") last = path.rfind(".") print(first, last, path[last + 1:], path[:first].upper()) What does the program print?
A class has a __str__ method, and a program joins its objects into one line.
class Tag:
def __init__(self, name):
self.name = name
def __str__(self):
return "#" + self.name
tags = [Tag("py"), Tag("oop")]
print(" ".join(str(t) for t in tags))
print(" ".join(tags))
What happens when the program runs?
This function should tell whether a text contains a tag. The program should print True False, but it prints False True.
def has_tag(text, tag):
if text.find(tag):
return True
return False
print(has_tag("#news today", "#news"), has_tag("plain", "#"))
Which one change makes it print True False?