مرکزی مواد پر جائیں

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

LessonTitleWhat You DoDuration
1Strings and f-StringsCreate strings, format data with f-strings, use escape sequences25 min
2Indexing and SlicingExtract characters and substrings using positions25 min
3String MethodsTransform text with methods, understand immutability20 min
4Collections as TypesWrite list, dict, tuple, and set annotations25 min
5Nested Types and Type SafetyRead nested types, use safe access patterns, SmartNotes TDG20 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.

StageWhat You DoClaude Code ToolWhat 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 predictionCalibrates your intuition for how strings and collections behave
RunPress Shift+Tab to exit Plan Mode. Execute the code or run uv run pyright, compare to your prediction.You run it, not Claude CodeCreates the feedback loop
InvestigateWrite a trace artifact explaining why your prediction was right or wrong. Use /investigate for Socratic questions./investigate probes mechanicsMakes your reasoning visible
ModifyChange an index, key, or method call and predict the new resultYou edit, Claude Code reviewsTests 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 generatesProves you can specify these types independently
Starter Kit Update

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.