Strings, Lists, and the Power of Python Collections
1 of 4
ICT · My World Quest — full scheme of work · pioneer
Strings, Lists, and the Power of Python Collections
3 more
1/4
For grown-ups
Companion summary
Have you ever wondered how Spotify builds your playlist, or how Google autocompletes your search?
Developers use strings and lists every single day to store names, messages, song titles, and search results.
In Python, just writing songs = ["Shape of You", "Blinding Lights"] instantly creates a collection you can sort, search, or shuffle.
When you feel confident, try writing a mini contact book that stores names in a list and greets each person by name.
✨
What's in the box?
🌍🌕☀️🚀
What's in the box?
Imagine a row of labelled boxes on a shelf. Box 0 holds your name, box 1 holds your favourite colour, box 2 holds your lucky number. You can reach into any box instantly just by knowing its number. Python uses exactly the same idea when it stores sequences of data. Before you dive in, think about this: if you had a sentence like 'hello', how many individual letters does it contain, and how would you grab just the third one? Jot your guess down — you'll be able to check it in a few minutes.
💭 Think about this
If the word 'hello' were stored in Python, which number do you think would point to the letter 'l' that appears first? Why?
✨
What's in the box?
🌍🌕☀️🚀
What's in the box?
Imagine a row of labelled boxes on a shelf. Box 0 holds your name, box 1 holds your favourite colour, box 2 holds your lucky number. You can reach into any box instantly just by knowing its number. Python uses exactly the same idea when it stores sequences of data. Before you dive in, think about this: if you had a sentence like 'hello', how many individual letters does it contain, and how would you grab just the third one? Jot your guess down — you'll be able to check it in a few minutes.
💭 Think about this
If the word 'hello' were stored in Python, which number do you think would point to the letter 'l' that appears first? Why?
💡
Strings and Lists in Python
🔢➕✖️🧮
Strings and Lists in Python
**What is a ?**
A string is a sequence of characters — letters, digits, spaces, or symbols — wrapped in quote marks. You can use single quotes or double quotes.
```python
greeting = 'hello'
message = "Python is fun!"
```
Every character in a string has an index — a position number that starts at 0, not 1. So in `'hello'`, index 0 is `'h'`, index 1 is `'e'`, index 2 is `'l'`, and so on. You can also count backwards: index -1 is always the last character.
Access a single character using square brackets:
```python
print(greeting[0]) # prints h
print(greeting[-1]) # prints o
```
Python has built-in string methods — actions you can call on a string using dot notation. A method is a built-in instruction that belongs to a specific data type. Useful ones include:
- `.upper()` — converts all characters to upper case
- `.lower()` — converts all characters to lower case
- `.replace(old, new)` — swaps one piece of text for another
- `.len()` is not a method but a function: `len(greeting)` returns 5
```python
print(greeting.upper()) # HELLO
print(message.replace('fun', 'great')) # Python is great!
```
**What is a ?**
A list is an ordered collection of items. Unlike a string (which only holds characters), a list can hold anything: numbers, strings, even other lists. Lists use square brackets, with items separated by commas.
```python
colours = ['red', 'green', 'blue']
scores = [10, 7, 14, 3]
```
Lists use the same zero-based ing as strings:
```python
print(colours[0]) # red
print(colours[-1]) # blue
```
You can change a list after you create it — this property is called mutability. A mutable object can be altered once it exists. Strings, by contrast, are immutable — you cannot change one character inside an existing string; you must build a new one.
Key list s:
- `.append(item)` — adds an item to the end
- `.remove(item)` — removes the first match of that item
- `.sort()` — sorts the list in ascending order
- `len(colours)` — returns the number of items
```python
colours.append('yellow') # ['red', 'green', 'blue', 'yellow']
colours.remove('green') # ['red', 'blue', 'yellow']
colours.sort() # ['blue', 'red', 'yellow']
```
Worked example
**Worked Example — step by step**
You are given this code:
```python
fruits = ['apple', 'banana', 'cherry']
fruits.append('date')
fruits.remove('banana')
print(fruits[1])
```
Step 1 — Start: `fruits = ['apple', 'banana', 'cherry']`
Step 2 — After `.append('date')`: `['apple', 'banana', 'cherry', 'date']`
Step 3 — After `.remove('banana')`: `['apple', 'cherry', 'date']`
Step 4 — `fruits[1]` accesses index 1 of the updated list, which is `'cherry'`.
Output printed: `cherry`
Key insight: always trace the list's current state before reading an index — the index values shift when items are added or removed.
🧠
Try It Yourself
Try it yourself
Read each question carefully. For code-trace questions, follow the code line by line in your head (or on paper) before choosing your answer.
1. What does the following code print?
```python
word = 'Python'
print(word[2])
```
2. Which of these correctly adds the number 5 to the end of an existing list called `nums`?
3. A student writes `name = 'Alice'` and then tries `name[0] = 'B'` to change the name to 'Blice'. What happens?
4. What is printed by this code?
```python
animals = ['cat', 'dog', 'fish', 'bird']
animals.remove('dog')
print(len(animals))
```
5. A programmer wants every letter in the string `city = 'london'` to appear in upper case. Which line of code achieves this?
Explain why your chosen answer is correct and the others are not.
📌
Bring It Together
Strings store sequences of characters and are immutable — you can read them but not change individual characters in place.
Lists store ordered collections of any items and are mutable — you can append, remove, and sort them after creation.
Both strings and lists use zero-based indexing, so the first item is always at index 0 and the last is always at index -1.
1. Trace this code and identify what is printed:
```python
data = ['sun', 'moon', 'star']
data.append('comet')
data.sort()
print(data[-1])
```
🌱 Reflect
Think about a real app you use — a playlist, a contacts list, a leaderboard. Which Python data structure (string or list) would best store that data, and which methods would be most useful? Write two sentences explaining your choice.