IS5312 Chap.2 Strings, Lists and Tuples
Strings, Lists and Tuples
This chapter treats strings, lists and tuples as related ordered sequences while keeping their different meanings and mutation rules visible. It covers positive and negative indices, excluded slice stops, stepped slices, methods and functions, split and join transformations, list operations, tuple conversion and nested access.
The central debugging distinction is between copying a list and assigning a second name to the same mutable object.
What this chapter covers
- 01
Positive and negative indexing
- 02
Slicing with start, stop and step
- 03
String methods, split and join
- 04
List mutation and sorting
- 05
Tuple immutability and list aliasing
Worked example · free
Clean a structured identifier
- 2Strip and normalise the text.
- 3Split and validate the fields.
- 2Join without losing leading zeros.
Key terms
- Slice
- A subsequence selected by start, stop and optional step values.
- Mutable Object
- An object whose contents can change in place.
- Aliasing
- Two variables referring to the same mutable object.
Strings, Lists and Tuples FAQ
Why is the slice stop excluded?
Python includes the start position and stops before the end position, making adjacent slices meet without overlap. This convention lets adjacent slices meet at one boundary without selecting the same item twice.
Why did sorting change both variables?
Both names referred to one list, so an in-place sort was visible through either name. Draw the single list object and connect both variable names before tracing the mutation.
When is a tuple preferable?
Use a tuple when an ordered group should not be changed in place and that restriction communicates the design. It communicates a stable grouping while preserving indexing, slicing and iteration behaviour.
Assessment move
Build sequence fluency on paper before relying on output. Start with a seven-character string and write positive indices above it and negative indices below it. Ask for the same position in both systems until the two coordinates feel interchangeable. Next, write six slices that vary one feature at a time: omitted start, omitted stop, positive step, negative step, stop beyond the sequence and full reversal.
Mark the selected positions and remember that the stop is excluded. Then repeat the exercise on a list and a tuple so the shared sequence behaviour is visible. After indexing, practise transformation chains. Take text with outside spaces, inconsistent case and a delimiter. Predict the value after strip, case normalisation, split and join, keeping each intermediate object on its own line.
Decide which digit groups are identifiers that must remain strings and which are quantities that need conversion. For mutation practice, draw one list object as a box and connect variable names to it with arrows. Trace ordinary assignment, slicing, sorted and the in-place sort method. After every operation, write what each variable shows and whether a new object exists.
Add one nested list and locate a target by writing one bracket step at a time; inspect the object returned by each step before adding another index. Keep an operation table with four columns: operation, valid object types, returns a new value, changes the original. Populate it with append, remove, sort, sorted, split, join, replace and tuple or list conversion.
The table should be built from executed examples, not memorised definitions. Finish each session with two original tasks. One should transform a sentence into a list of cleaned words and back into one display string. The other should copy an editable list, change only the copy and prove the original remained unchanged. Test empty sequences, a one-item sequence, a negative index at the left boundary and an out-of-range slice.
For the lab test, the critical habit is to state the object identity as well as the displayed value. Many sequence errors look correct until a later mutation reveals that two variables shared one list. Add a prediction grid for each new operation. Record the original object, the expression, the returned value and the object after execution.
This distinguishes a method that mutates from a function that creates a new result, even when both displays look identical. Include a case with duplicate values because remove acts on a matching item rather than on every duplicate, and include a slice whose stop lies beyond the right edge because slicing and indexing respond differently to that boundary.
For nested sequences, narrate each selection as a type transition: outer list to inner record, then record to field. If a transformation ends with join, verify that every item is text before joining. Close the session by producing two independent copies in different ways and mutating each copy once. Compare identities and contents after every change.
The desired outcome is not merely the correct final list; it is a clear explanation of which operation created a new object, which preserved order and which changed shared state.