COMP90059 Chap.4 Strings and Sequence Operations
Strings and Sequence Operations
Strings and lists share the sequence model: ordered items have positions, positive indices begin at zero and negative indices count from the end. Indexing retrieves one required item, while slicing retrieves a half-open region whose stop is excluded. This makes adjacent prefix and suffix slices fit without overlap. Strings are immutable, so methods return new strings rather than changing the original characters.
Search operations also have contracts: membership returns a Boolean, while find returns a position or the sentinel negative one. Nested access should be evaluated one bracket at a time because each bracket acts on the object returned by the previous lookup. List methods add another distinction between mutation and return values.
What this chapter covers
- 01
Positive indices count from zero
- 02
Negative indices count backward from the final item
- 03
Indexing requires a valid single position
- 04
Slicing includes the start and excludes the stop
- 05
A step controls spacing and traversal direction
- 06
String methods return new immutable strings
- 07
Find uses negative one to represent a missing substring
- 08
Nested brackets cross one sequence object at a time
- 09
Membership tests answer presence without revealing a position
- 10
Split and join convert between structured fields and textual records
- 11
Slice copies are new outer sequence values rather than aliases
- 12
Normalization should precede comparison when case or whitespace is irrelevant
Worked example · free
Clean and split a compact code
- 1Strip surrounding whitespace to obtain ab-204.
- 1Call upper and assign the returned string to obtain AB-204.
- 1Find the hyphen at index 2 and check that the result is not negative one.
- 1Slice before the hyphen to obtain AB.
- 1Slice from the character after the hyphen to obtain 204.
Key terms
- Sequence
- An ordered collection whose items can be visited in order and often accessed by position.
- Positive index
- A zero-based position counted from the beginning of a sequence.
- Negative index
- A position counted backward from the end, with negative one naming the final item.
- Half-open slice
- A sequence region that includes its start index and excludes its stop index.
- Immutable string
- A string whose character sequence cannot be changed in place after creation.
- Sentinel value
- A distinguished result such as negative one that signals an expected special state like not found.
- String normalization
- Applying a documented transformation such as trimming whitespace or changing case before comparison so irrelevant textual variation is removed.
- Membership test
- An in or not-in expression that reports whether a value occurs in a sequence without promising the position of an occurrence.
- Delimiter
- A character or substring that separates fields in text and provides the boundary used by split or join operations.
Strings and Sequence Operations FAQ
Why is the stop index excluded?
Half-open slices make the slice length equal stop minus start and let adjacent slices join without duplicating the boundary item, which simplifies both reasoning and loop boundaries.
Why can a large slice succeed when a large index fails?
An index promises one existing item, so an unavailable position is an error. A slice describes an available region and safely stops at the sequence end.
Do string methods change the original text?
No. Strings are immutable, so methods return new strings. Assign the returned value when the transformed text should be retained, and keep the original binding when both forms are needed.
Why is a missing find result dangerous?
Find returns negative one, and negative one is also a valid index for the last item. Check the sentinel before using it as a position.
When should I use membership rather than find?
Use membership when the only question is whether a substring occurs. Use find when the position is needed, and immediately handle its missing-value sentinel before indexing or slicing. This keeps a legitimate negative index from disguising a failed search.
How can I debug a nested indexing expression?
Evaluate one bracket at a time and write down the intermediate object, its type and its length. Confirm that each next index is valid for that particular object, not the original collection. For a slice, list the selected positions; for a method call, record whether it returns a new string or a sentinel requiring a branch.
Exam move
Write an index strip above a six-character string, with positive positions above and negative positions below. Evaluate several slices by listing selected indices first. Include an empty slice, a stop beyond the end and a negative step. Then build a cleaning pipeline that strips text, changes case, searches for a marker and splits the string only when the marker exists.
For nested lists and strings, write the intermediate object after each bracket. Finish by contrasting string methods that return new text with list methods that mutate and may return None. This routine turns compact bracket syntax into visible, typed steps.
Working through Strings and Sequence Operations in COMP90059? Sia is AskSia’s AI Computer Science tutor — ask any COMP90059 Strings and Sequence Operations question and get a clear, step-by-step explanation grounded in how COMP90059 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.