๐ŸŸข LESSON 02 ยท MOSTLY GUIDED

Length & Indexing

Learn to measure strings with len(), and pick out individual characters using index positions. Some code is provided โ€” you fill in the key parts!

๐ŸŸข Starter code provided โ€” fill in the missing sections

๐ŸŽฏ Learning Objectives

๐Ÿ”ฌ String Index Explorer ACTIVITY 1

word = "PYTHON"

P
0
-6
Y
1
-5
T
2
-4
H
3
-3
O
4
-2
N
5
-1

โฌ†๏ธ Yellow numbers = positive index (0 to 5)  |  Blue numbers = negative index (-6 to -1)

โœ‚๏ธ Slice Explorer ACTIVITY 2

Type start and end values to see what slice you get from "PYTHON". Try different combinations!

word = "PYTHON"

"PYT"
word[0:3] โ†’ "PYT"

๐Ÿ’ป Starter Code โ€” Fill the Gaps ACTIVITY 3
๐Ÿ“ Create input.txt โ€” Open Notepad, paste the text below, and save as input.txt in the same folder as your Python file.
TEXT โ€” input.txt
Hello World
Python Programming
GCSE Computer Science
Jersey Channel Islands
The cat sat on the mat
Edexcel 1CP2 Specification
Your task: The code below has gaps โ€” work out what goes in each ___ gap. Hints are in the comments. Once you figure it out, type it in IDLE and run it!
Python โ€” lesson2.py (complete the gaps in IDLE)
# Lesson 2 โ€” Length and Indexing
# Fill in the ___ gaps before running!

input_file  = open("input.txt", "r")
output_file = open("output.txt", "w")

for line in input_file:
    line = line.strip()

    # Find the length of the line โ€” how many characters?
    length = ___(line)           # HINT: use len()

    # Get the first character (index 0)
    first_char = line[___]        # HINT: what's the index of the first item?

    # Get the last character using negative indexing
    last_char = line[___]         # HINT: -1 gives the last character

    # Get the first 3 characters using slicing
    first3 = line[___:___]        # HINT: start at 0, stop at 3

    print(f"Line: {line}")
    print(f"Length: {length} | First: {first_char} | Last: {last_char} | First3: {first3}")

    output_file.write(f"{line} โ€” {length} chars\n")

input_file.close()
output_file.close()
print("Done!")

๐Ÿ› Fix the Bugs ACTIVITY 4

This code has 4 bugs. Click on each red underlined section to reveal the fix.

myfile = open("input.txt", "R") for line in myfile: line = line.strip() length = len line first = line[1] last = line[0] print(f"Length:{length}, First:{first}, Last:{last}") myfile.close()
Bugs found: 0 / 4

๐Ÿง  Quick Quiz ACTIVITY 5
If word = "Jersey", what does len(word) return?
5
6
7
0
If s = "Python", what is s[0]?
"y"
"P"
"n"
An error
If s = "GCSE", what is s[1:3]?
"GCS"
"CSE"
"CS"
"GC"
If word = "Code", what does word[-1] return?
"C"
"-1"
"e"
An error

๐Ÿ”ฎ Predict the Output ACTIVITY 6

Look at each code snippet and predict what will be printed. Click to reveal!

What does this print?
word = "Python"
print(len(word))
5
6
7
An error
What does this print?
s = "Hello"
print(s[1] + s[4])
Ho
eo
He
lo
What does this print?
word = "Computing"
print(word[0:4])
Comp
Compu
ompu
Com
What does this print?
name = "Jersey"
print(name[-2])
y
e
J
r

๐ŸŽฎ Memory Game ACTIVITY 7

Match each function to its description. Click two cards to flip them!

Moves: 0Pairs: 0/5

๐Ÿš€ Extension Activity STRETCH
Challenge: Reverse String Builder

Write a program that reads each line from input.txt and creates a reversed version of the string using slicing.

Hint: In Python, word[::-1] reverses a string!

Steps:
1. Read each line from input.txt
2. Reverse each line using line[::-1]
3. Write to output.txt: the original line, the reversed line, and the length
4. Check: Is any line a palindrome (reads the same forwards and backwards)?

Extra challenge: Use line[::2] to get every other character. What does line[1::2] do?
โ† Lesson 1 Lesson 3: Finding & Searching โ†’