Skip to main content

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

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 DoWhat 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
RunExecute the code, compare to your predictionCreates the feedback loop
InvestigateWrite a sentence explaining why your prediction was right or wrongMakes your reasoning visible
ModifyChange an index, key, or method call and predict the new resultTests whether your understanding transfers
Make [Mastery Gate]Write string operations or collection declarations from scratchProves 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.