Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

In a predict and run task you do not write code from scratch. You start from a small piece of working code, work out what you think it will do before you run it, and then run it to check whether you were right.

This task contains thirteen short examples in five groups, one group for each part of string manipulation. Work through them in order.

Predict

For every example, study the code carefully and write down a prediction before you touch the Run button. Your prediction should use the key terms string, character and index, and it should give the exact output, character for character. Spaces count, so say where they are. You can type your prediction straight into the code as a comment, on a line starting with #.

Only once you have written your prediction should you run the code and compare the result with what you expected. Where the two differ, work out why before you open the explanation underneath.

Need more help with strings? Click here.

String length

Example 1

text_length = len("Dave")
print(text_length)

text_length = len("Lovelace")
print(text_length)

name = "Turing"
print(len(name))

name = "Lovelace"
if len(name) > 7:
    print("Long name")
else:
    print("Short name")

Predict all four lines of output. For the last one, say what the condition works out to before you decide which branch runs.

Upper and lower case

Example 2

text_one = "Python Code"
text_two = "123 hello!"

result_a = text_one.upper()
result_b = text_one.lower()
result_c = text_two.upper()

print(result_a)
print(result_b)
print(result_c)
print(text_one)

Predict all four lines. Think carefully about the digits and the punctuation in text_two, and about what the last line prints after text_one has been converted twice.

What happened, and why
PYTHON CODE
python code
123 HELLO!
Python Code

Only letters have a case, so 123 and ! come through the conversion unchanged.

The last line is the one to look at. upper() and lower() build a new string and hand it back; the new strings were stored in result_a and result_b. Nothing was ever assigned back to text_one, so it still holds exactly what it held on the first line.

Example 3

word_one = "PYTHON"
word_two = "python"
word_three = "Python 3.10"

check_a = word_one.isupper()
check_b = word_two.islower()
check_c = word_three.isupper()
check_d = word_three.islower()

print(check_a)
print(check_b)
print(check_c)
print(check_d)

These four do not convert anything: each one checks a string and gives back True or False. Predict all four.

What happened, and why
True
True
False
False

PYTHON is all capitals and python is all small letters, so the first two checks are True.

Python 3.10 is the interesting one: it gives False for both checks. It has one capital letter and five small ones, so it is not all capitals, and it is not all small letters either. A string with a mix of the two is neither.

The digits, the space and the full stop play no part in the answer, because they have no case.

Substrings

Example 4

text = "I saw a wolf in the forest, a lonely wolf."
sub_string = text[20:26]

print(sub_string)

Count the characters from index 0, spaces included, to find index 20. Then predict how many characters the slice gives you, and which ones.

What happened, and why

The program prints forest.

Counting from 0, the f of forest is at index 20 and its last letter, t, is at index 25. The slice stops before index 26, which is the comma, so the comma is left out. 26 - 20 is 6, which is exactly the number of letters in forest.

Left and right

Example 5

text = "I saw a wolf in the forest, a lonely wolf"
sub_string = text[:6]
print(sub_string)

The colon comes first, so this is a slice from the left. Predict exactly which six characters come out.

What happened, and why

The program prints I saw followed by a space, which you cannot see.

text[:6] is the left six characters, indices 0 to 5: I, a space, s, a, w and another space. The output looks like five characters, but the sixth is there. Add print(len(sub_string)) underneath and it prints 6.

Example 6

text = "I saw a wolf in the forest, a lonely wolf"
sub_string = text[20:]

print(sub_string)

This time the colon comes last. Predict where the output starts and where it ends.

Example 7

text = "I saw a wolf in the forest, a lonely wolf"
sub_string = text[-8:]

print(sub_string)

A negative number before the colon takes characters from the right. Predict exactly which eight characters you get, counting back from the end.

What happened, and why

The program prints ely wolf.

It is tempting to predict lonely wolf, but that is eleven characters. The right eight characters are the four letters of wolf, the space in front of it, and the last three letters of lonely. When a right-hand substring has to be a whole word, count the characters back from the end, space included.

Converting to and from ASCII

Example 8

message = ""

message = message + chr(72)
message = message + chr(101)
message = message + chr(108)
message = message + chr(108)
message = message + chr(111)

print(message)

message starts out empty and gains one character on each line. Use the ASCII ranges from the theory to predict which one each time, then predict the word.

Example 9

character_code = ord("A")
print(character_code)

Predict the number. It is one you have met in the theory, so try to remember it before you look it up.

Example 10

character_code = ord("!")
print(character_code)

Punctuation has a code too. You are not expected to know this one, so predict whether it is bigger or smaller than the code for A.

Example 11

character_code = ord("6")
print(character_code)

Predict the output, and be careful: the 6 is in speech marks.

What happened, and why

The program prints 54, not 6.

"6" is a string: the character six, as it appears on a keyboard. ord gives its ASCII code, and the codes for the digits start at 48 for 0, so the character 6 is 48 + 6 = 54. The number six and the character 6 are two different things, which is exactly the difference int() bridges when it turns typed input into a number.

Example 12

name = "Turing"

character_code = ord(name[2])
print(character_code)

Two steps happen on one line here. Predict which character name[2] picks out first, then its code.

What happened, and why

The program prints 114.

Python works from the inside out. name[2] is the character at index 2, and counting from 0 that is the r (T is 0, u is 1, r is 2). ord("r") is

  1. A prediction of 117 means the counting started at 1, which picks out the u.

Example 13

name = "Irfan"
ascii_total = 0

for letter in name:
    ascii_total = ascii_total + ord(letter)

print(ascii_total)

Say how many times the loop runs, and what letter holds on each of those times. You do not have to predict the final number exactly, but predict how many lines of output there are, and why.

What happened, and why

The program prints one line, 496.

The loop runs five times, once for each character of Irfan, and each time letter holds the next character: I, r, f, a, n. Their codes are 73, 114, 102, 97 and 110, and the running total adds them up to 496.

There is only one line of output because the print is not indented: it runs once, after the loop has finished. Indent it by four spaces and the program prints the running total five times instead, ending on the same 496.

Help! My code doesn't work!

Make sure that you check for the following things:

  • String indexing starts at zero. Are you counting properly?
  • Spaces and punctuation have an index too, so count them.
  • The second number of a slice is never included.
  • The colon is on the correct side of the number: [:6] for the left, [-8:] for the right.
  • upper() and lower() give back a new string and leave the original alone.
  • ord turns one character into a number, and chr turns a number into one character.