9.16 Hands-on Exercises
Log Level Analyzer Build a function that scans a batch of log lines and tallies how many fall into each severity level — an at-a-glance …
Log Level Analyzer Build a function that scans a batch of log lines and tallies how many fall into each severity level — an at-a-glance …
Conceptual Questions Why are Python strings immutable, and what does that mean for methods like .replace()? What’s the difference …
Assuming a Method Mutates in Place Every string method returns a new string — the original is never changed, since str is immutable (see 9.1 …
Build Large Strings with join(), Not += Accumulating a string with += inside a loop is O(n²); collecting pieces in a list and calling …
Almost everything touched in cloud and infrastructure automation is a string: log lines, Amazon Resource Names (ARNs), IAM policy documents, …
Why Immutability Matters Here Immutability makes strings safe to share across functions, threads, and dict/set keys without defensive …
What Is Regex? A regular expression is a mini-language for describing text patterns — used for validation, extraction, and bulk …
flowchart TD SA["String Algorithms"] SA --> CH["Check\npalindrome, anagram"] SA --> TR["Transform\nreverse, compress"] SA --> …
These are global built-in functions that accept a string as an argument, rather than methods called on the string object itself (len(s) vs. …
Every method below returns a new string (or list/tuple) — none modify the original, consistent with 9.1 Introduction to Strings. Case …
Three Ways to Format flowchart TD A["% operator\nlegacy, printf-style"] --> D["Same output"] B[".format()\nPython 2.7+, readable"] --> D …
Concatenation (+) Joins two strings end-to-end into a new string — the simplest way to build text from pieces, used constantly for messages, …
Indexing and Negative Indexing Every character in a string has a position. Positive indices count from the left starting at 0; negative …
The core escape sequences (\n, \t, \\, quotes) were introduced in 4.13 Escape Characters. This is the complete reference, including the …
Single and Double Quotes Single and double quotes are functionally identical — the choice is purely stylistic, except when the text itself …
flowchart TD S["str"] S --> C["Creation\nquotes, raw, unicode"] S --> I["Indexing & Slicing"] S --> F["Formatting\n% .format f-strings"] S …
Concatenation FIRST="Hello" SECOND="World" GREETING="$FIRST $SECOND" # simple concatenation via adjacency …