Chapter 48: Strings and Collections
James stares at his SmartNotes code. He has a note with a title, a word count, and a draft flag, all properly typed from Chapter 47. Now he needs tags. He types:
tags: str = "python,ai,beginner"
Emma glances at his screen. "How do you check if a note has the 'ai' tag?"
James thinks for a moment. "I could search the string for 'ai'."
"What if the title contains the word 'ai' but it is not a tag? What if someone adds a tag called 'rail' and your search finds 'ai' inside it?"
James sees the problem. A comma-separated string is not a list; it just looks like one. Emma replaces his line:
tags: list[str] = ["python", "ai", "beginner"]
"Now 'ai' in tags checks actual tags, not substrings. And pyright knows every element is a str."
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 | What It Builds |
|---|---|---|
| Predict [AI-FREE] | Predict string operation results or collection access values, with a confidence score (1-5) | Calibrates your intuition for how strings and collections behave |
| Run | Execute the code, compare to your prediction | Creates the feedback loop |
| Investigate | Write a sentence explaining why your prediction was right or wrong | Makes your reasoning visible |
| Modify | Change an index, key, or method call and predict the new result | Tests whether your understanding transfers |
| Make [Mastery Gate] | Write string operations or collection declarations from scratch | Proves you can specify these types independently |
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.