QuestenaPractice that shows what to review next
Topic lesson

Strings: trace text operations without mutation

About 5 min

Questions stay in the language in which they were published.

These questions ask what a text expression measures or produces and whether the original string changes. Work from the string's sequence model, then apply the exact operation rather than guessing from how the result looks.

Treat text as an immutable Unicode sequence

A string stores Unicode text as a sequence of code points. Single and double quotes are alternative delimiters for the same str type, and indexing produces another str containing one code point rather than a separate character type. Try word = 'map' followed by word[0]: the selected value is still a string.

The length of a string counts code points, not the bytes produced by a later encoding and not necessarily the visual symbols perceived by a reader. Because strings are immutable, concatenation, repetition and ordinary methods create string values without altering their operands. Try new = 'go' + 'al': new receives combined text while both source values remain unchanged.

OperationMental model
part in textSearch for one contiguous substring; word boundaries and object identity are irrelevant.
text times a nonnegative integerRepeat the complete sequence that many times.
strip with no argumentRemove whitespace from the two outer edges only.
split with no separatorTreat each run of whitespace as one boundary and omit empty edge fields.
separator.join(parts)Require string elements and insert the separator between neighbours, never after the final element.

A formatted string evaluates each replacement expression, formats the resulting value, and builds new text. The field may contain an expression rather than only a name. Try total = 2 + 3: a replacement field can evaluate that addition before its value appears in the new string.

Reject the tempting wrong routes

TrapHow to reject it
Treating strings as mutable ASCII byte arrays or inventing a character typeReturn to the Unicode sequence model: positions hold code points, indexing returns str, and existing positions cannot be edited in place.
Expecting concatenation or a case-changing method to modify its receiverTrack the returned string separately from the original. Immutability rules out an in-place text edit.
Removing spaces from the middle when strip is calledInspect only the beginning and end. Internal whitespace survives the no-argument call.
Creating empty fields for every space during a no-argument splitCollapse consecutive whitespace into one boundary and ignore whitespace at the edges.
Adding a separator after the final elementPlace the separator only between adjacent string elements.

Try it

Question 15

For a Python string, what does `len(text)` count?

  1. The number of UTF-8 bytes needed after encoding
  2. The number of visual symbols every reader perceives
  3. The number of Unicode code points in the string
  4. The number of ASCII characters, excluding other text
Question 18

What is the value of `"ab" * 3`?

  1. `abbb`
  2. `ababab`
  3. `ab3`
  4. A TypeError is raised.
Question 24

For Python formatted string literals, which statements are true? Select all that apply.

  1. Replacement fields remain unchanged as literal text.
  2. A field may contain an expression rather than only a name.
  3. The source string is mutated after a field is evaluated.
  4. Each replacement expression is evaluated before its value is formatted.
Start drillPractice this topic
Strings: trace text operations without mutation · Questena