City University of Hong Kong · FACULTY OF INFORMATION TECHNOLOGY

IS5312 Chap.2 Strings, Lists and Tuples

- one subject, every graph, every model, every mark
5 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 2 of 6 · IS5312

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.

In this chapter

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

Q [7 marks]. The marks shown are an AskSia study allocation and are not the University marking scheme. Transform a spaced, lower-case service identifier into a stable display form.
Positive and negative indices name the same positions.Positive and negative indices name the same positions. Positive indices count from zero at the left, while negative indices count backward from minus one at the right.One sequence, two coordinate systemspositivenegativeP0-7Y1-6T2-5H3-4O4-3N5-2!6-1A slice stops before its end index, in either direction.
Positive and negative indices name the same positions. Positive indices count from zero at the left, while negative indices count backward from minus one at the right.
  • 2Strip and normalise the text.
  • 3Split and validate the fields.
  • 2Join without losing leading zeros.
Strip outside spaces, convert letters to upper case, split on the delimiter and confirm the expected field count. Keep the serial as text so leading zeros survive, then join the fields with the required display separator. Its score is an AskSia-authored study allocation and not a published University rubric.
Sia tip — Digits are text when arithmetic has no meaning.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 31 of your subjects at City University of Hong Kong, plus 1,000+ more Bibles across the AskSia library.
Sia - your IS5312 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works