Strings, Lists, and Tuples
Store ordered items and use positions without confusing a value with its index.
python, strings, lists, tuples, sequences, indexingPut items in order
A row of labelled lockers has positions. Python sequences also keep an
order: text uses a str, a changeable collection can use a list, and a
tuple holds an ordered, fixed sequence of references. For example:
index: 0 1 2
list: "red" "blue" "green"
Index 0 is the first item, not the value zero. items[-1] reads the last
item. An out-of-range index raises IndexError. len(items) counts items.
Choose a sequence
| Type | Example | Change an existing item directly? |
|---|---|---|
| String | "cat" | No |
| List | [1, 2] | Yes |
| Tuple | (1, 2) | No |
Strings and tuples are immutable, though a tuple can contain a mutable
object. A list can hold values of different types. A comma makes a single-item
tuple: (4,). A slice such as items[1:3] includes index 1 but excludes 3.
Python illustration
word = "cat"
colours = ["red", "blue", "green"]
point = (4, 7)
colours[1] = "gold"
colours.append("white")
print(word[0])
print(colours[1:3])
print(point[-1])
Prepared output — not live execution:
c
['gold', 'green']
7
Appending adds at the end; it does not sort the list. Assignment to an existing index replaces an item; it does not insert one.
See the reasoning
Follow the example one step at a time
Follow the list and slice in the lesson's example. This is a prepared teaching demonstration, not live code execution.
Start
index: 0 1 2 colours: ["red", "blue", "green"]
Python labels the first position index 0.
Explore the shared foundations
See Arrays and Lists for more list operations and costs, and Strings for text, indexing and limitations. Neither character count nor code-point position necessarily equals a user-perceived character when text contains combining marks.
Reflect on the concept
Four fixed questions to reinforce the reasoning. Answers are not saved or scored.
