Phase 2: Programming Fundamentals II · ~35 minutes · Python · uv
Strings and Text Processing
s[0] = "H" fails because strings never change in place. Every string method you call returns a brand-new string.
Hiring signal: Knows strings are immutable and reasons correctly about which string method call actually does what's needed
What you will learn
- Use core string methods: .split(), .strip(), .join(), .replace(), and f-strings
- Explain why strings are immutable and why every 'modification' actually creates a new string
- Diagnose a real TypeError from attempting in-place string mutation
- Parse unstructured text into structured pieces using .split()
Introduction
Type: Learn Languages: Python Prerequisites: Lesson 02 (Dictionaries and Sets) Time: ~35 minutes
Objective
Learning objectives
- Use core string methods:
.split(), .strip(), .join(), .replace(), and f-strings - Explain why strings are immutable and why every "modification" actually creates a new string
- Diagnose a real
TypeError from attempting in-place string mutation - Parse unstructured text into structured pieces using
.split()
What you're building
A script (log_parser.py) that:
- Takes at least 3 raw log lines in the format
TIMESTAMP LEVEL MESSAGE (space-separated, message may contain spaces), hardcoded as strings - Parses each into a dictionary with keys
"timestamp", "level", and "message", using .split(" ", N) with the correct limit so multi-word messages stay intact - Prints each parsed line using an f-string in a clean, readable format
- Contains a comment demonstrating the
s[0] = "X" TypeError and explaining, in your own words, why .replace() is the correct tool instead
What does " data science ".strip().replace(" ", "_") return?
.strip() runs first, removing only the leading and trailing whitespace: "data science" (the single space between the words stays, since .strip() only touches the outer edges). .replace(" ", "_") then runs on that result, converting every remaining space — just the one in the middle — to an underscore: "data_science". Order matters here: running .replace() before .strip() would still work in this case, but chaining methods left-to-right, each operating on the previous result, is the general pattern worth internalizing.
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Problem, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy