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.
| Operation | Mental model |
|---|---|
| part in text | Search for one contiguous substring; word boundaries and object identity are irrelevant. |
| text times a nonnegative integer | Repeat the complete sequence that many times. |
| strip with no argument | Remove whitespace from the two outer edges only. |
| split with no separator | Treat 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
| Trap | How to reject it |
|---|---|
| Treating strings as mutable ASCII byte arrays or inventing a character type | Return 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 receiver | Track the returned string separately from the original. Immutability rules out an in-place text edit. |
| Removing spaces from the middle when strip is called | Inspect only the beginning and end. Internal whitespace survives the no-argument call. |
| Creating empty fields for every space during a no-argument split | Collapse consecutive whitespace into one boundary and ignore whitespace at the edges. |
| Adding a separator after the final element | Place the separator only between adjacent string elements. |
Try it
For a Python string, what does `len(text)` count?
- The number of UTF-8 bytes needed after encoding
- The number of visual symbols every reader perceives
- The number of Unicode code points in the string
- The number of ASCII characters, excluding other text
What is the value of `"ab" * 3`?
- `abbb`
- `ababab`
- `ab3`
- A TypeError is raised.
For Python formatted string literals, which statements are true? Select all that apply.
- Replacement fields remain unchanged as literal text.
- A field may contain an expression rather than only a name.
- The source string is mutated after a field is evaluated.
- Each replacement expression is evaluated before its value is formatted.