Chapter 48: Strings and Collections
James has a SmartNotes note with a title, a word count, and a draft flag, all properly typed from Chapter 47. Now he needs to store three tags for a note. He tries:
tag1: str = "python"
tag2: str = "ai"
tag3: str = "beginner"
Three separate variables for three tags. Emma asks, "What if a note has ten tags? Or twenty? Will you create tag1 through tag20?"
James sees the problem immediately. He cannot create a new variable every time someone adds a tag. He needs one variable that holds all the tags together.
That is exactly what a list does:
tags: list[str] = ["python", "ai", "beginner"]
One variable, three values. If someone adds a fourth tag, the list grows. If they remove one, it shrinks. And the type annotation list[str] tells both pyright and any AI reading this code: "this variable holds a collection of strings."
Strings hold one piece of text. Lists hold many pieces. That is the jump this chapter makes.
This chapter gives you two things Chapter 47 did not cover: string operations (how to work with text) and collection types (how to group values together). By the end, you will write list[str], dict[str, int], tuple[float, float], and set[str] as naturally as you write int and str. These types become your specification vocabulary when you tell AI what data a function should accept and return.
What You Will Learn
By the end of this chapter, you will be able to:
- Create strings with three quote styles and format data with f-strings
- Extract characters and substrings using indexing and slicing
- Transform strings with methods like
.strip(),.split(), and.replace() - Write type annotations for lists, dicts, tuples, and sets
- Read nested type annotations like
list[dict[str, int]]in AI-generated code - Use safe access patterns like
.get()to avoid runtime errors
Chapter Lessons
| Lesson | Title | What You Do | Duration |
|---|---|---|---|
| 1 | Strings and f-Strings | Create strings, format data with f-strings, use escape sequences | 25 min |
| 2 | Indexing and Slicing | Extract characters and substrings using positions | 25 min |
| 3 | String Methods | Transform text with methods, understand immutability | 20 min |
| 4 | Collections as Types | Write list, dict, tuple, and set annotations | 25 min |
| 5 | Nested Types and Type Safety | Read nested types, use safe access patterns, SmartNotes TDG | 20 min |
PRIMM-AI+ in This Chapter
Every lesson includes a PRIMM-AI+ Practice section. You are now working with strings and collections, so predictions involve indexing results, method outputs, and collection access patterns.
| Stage | What You Do | Claude Code Tool | What It Builds |
|---|---|---|---|
| Predict [AI-FREE] | Predict string operation results or collection access values, with a confidence score (1-5). Press Shift+Tab for Plan Mode. | Plan Mode protects prediction | Calibrates your intuition for how strings and collections behave |
| Run | Press Shift+Tab to exit Plan Mode. Execute the code or run uv run pyright, compare to your prediction. | You run it, not Claude Code | Creates the feedback loop |
| Investigate | Write a trace artifact explaining why your prediction was right or wrong. Use /investigate for Socratic questions. | /investigate probes mechanics | Makes your reasoning visible |
| Modify | Change an index, key, or method call and predict the new result | You edit, Claude Code reviews | Tests whether your understanding transfers |
| Make [Mastery Gate] | Write string operations or collection declarations from scratch. Use /tdg for TDG cycles (Lesson 5). | Your work, Claude Code generates | Proves you can specify these types independently |
If you installed the PRIMM-AI+ starter kit in Chapter 42, your /predict, /investigate, and /bug commands still work. This chapter also uses /tdg for TDG cycles in Lesson 5. If you don't have it, update your kit from panaversity/primm-ai-starter-kit.
The SmartNotes Connection
At the end of this chapter, you will define SmartNotes data using collections: tags: list[str], metadata: dict[str, str], and notes: list[str]. Then you will write a format_tag_list(tags: list[str]) -> str stub and let AI generate the body using ", ".join(tags). This is your first function that takes a collection and returns a string, a pattern you will use throughout SmartNotes.