Skip to main content
SingaporeComputer ScienceSyllabus dot point

How do Python lists and strings store and index sequences of data?

Use Python lists and strings, including indexing, length, looping over items, and basic operations like append and slicing

A focused answer to the O-Level Computing point on Python lists and strings. Creating lists, zero-based indexing, finding length with len(), looping over items, appending, and basic string operations and slicing.

Generated by Claude Opus 4.87 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to use Python lists and strings: creating them, indexing items, finding their length, looping over them, and basic operations such as append and slicing. The central idea is that both a list and a string are ordered sequences whose items have positions (indexes) starting at 00, so you can reach any item by its index and process every item with a loop.

The answer

Creating a list

A list stores several values in order, written in square brackets:

scores = [40, 75, 60, 90]
names = ["Ravi", "Mei", "Tom"]

A list can hold numbers, strings, or a mix, and it can grow or shrink.

Indexing starts at zero

Each item has an index showing its position, starting at 00:

scores[0]    # 40  (first item)
scores[3]    # 90  (fourth item)

The last item of a list of length nn is at index n−1n - 1. A negative index counts from the end: scores[-1] is the last item.

Length with len()

len() gives the number of items in a list or characters in a string:

len(scores)    # 4
len("cat")     # 3

Looping over items

A for loop visits each item in turn:

for score in scores:
    print(score)

Adding to a list

append adds an item to the end:

scores.append(55)    # scores is now [40, 75, 60, 90, 55]

Strings are sequences too

A string is a sequence of characters, indexed the same way:

word = "COMPUTER"
word[0]      # 'C'
len(word)    # 8

Slicing

A slice takes part of a list or string with [start:stop]. The start is included, the stop is excluded:

word[0:4]    # 'COMP'  (characters 0,1,2,3)
scores[1:3]  # [75, 60]

Examples in context

Example 1. A register of names. A teacher stores student names in a list and loops over it to print the register. Adding a new student is a single append, and len(names) gives the class size at any time without counting by hand. Because the names keep their order, the first to register is always names[0], which makes it easy to process them in the order they arrived.

Example 2. Checking a password length. A login program uses len(password) to reject passwords shorter than eight characters, and could slice password[0:3] to show a masked hint such as the first three letters. Treating the password as a sequence of characters makes both the length check and the slice short and clear, and the same indexing ideas apply to any text the program reads.

Try this

Q1. For data = [10, 20, 30], state the value of data[1]. [1 mark]

  • Cue. 2020, the second item (indexing starts at 00).

Q2. Write a line that adds the number 9999 to the end of a list called nums. [2 marks]

  • Cue. nums.append(99).

Q3. State what len("HELLO") returns and why. [2 marks]

  • Cue. 55, because the string has five characters and len counts them.

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original5 marksA list is created with `scores = [40, 75, 60, 90]`. (a) State the value of `scores[0]` and `scores[3]`. (b) Write a for loop that prints each score. (c) Write a line that adds the score 5555 to the end of the list.
Show worked answer →

(a) Indexing starts at 00, so scores[0] is the first item, 4040, and scores[3] is the fourth item, 9090.

(b) Loop directly over the list:

for score in scores:
    print(score)

(c) Use append to add to the end:

scores.append(55)

After this, scores is [40, 75, 60, 90, 55].

Markers reward scores[0] = 4040 and scores[3] = 9090 (zero-based), a for loop over the list, and append(55) adding to the end.

Original5 marksA variable holds `word = 'COMPUTER'`. (a) State the value of `word[0]` and `len(word)`. (b) Write a for loop that prints each character on its own line. (c) State what `word[0:4]` produces.
Show worked answer →

(a) word[0] is the first character, 'C' (indexing starts at 00). len(word) is the number of characters, which is 88.

(b) Loop over the string:

for letter in word:
    print(letter)

(c) word[0:4] is a slice from index 00 up to (but not including) index 44, giving the first four characters, 'COMP'.

Markers reward word[0] = 'C' and len(word) = 88, a for loop over the characters, and the slice 'COMP' (stop index excluded).

Related dot points