Strings & String Formatting
🔤 Strings & String Formatting
.format(), and old-style % — with a clear recommendation for which one to actually reach for.
✂️ String Indexing & Slicing
Slice syntax is [start:stop:step] — the same shape you'll see again on lists in Chapter 6. stop is exclusive, matching range() from the last chapter.
String Indexing: Go vs Kotlin vs Python
| Language | Indexing a string |
|---|---|
| Go | A string is a byte slice — indexing with s[0] gives a raw byte, not a character. Multi-byte UTF-8 characters (like emoji or accented letters) require iterating with range to get correct character boundaries. |
| Kotlin | s[0] gives a Char — indexing is character-based like Python, inherited from Java's String behavior. |
| Python | s[0] gives a single-character string, always Unicode-aware — no separate "byte vs character" distinction to worry about at this level. |
🔒 Strings Are Immutable
A string can't be changed in place — every string method that looks like it's "modifying" a string is actually returning a brand-new string:
🛠️ Common String Methods
"-".join(["a", "b", "c"]) reads backwards to most newcomers — the separator string comes first, and the list of pieces to join is the argument. Building a string by repeatedly concatenating with + inside a loop works, but "".join(...) is the idiomatic and more efficient way to combine many pieces at once, since strings are immutable and each + creates yet another new string.
📝 f-strings: Formatted String Literals
An f-string embeds expressions directly inside a string, prefixed with f:
📐 .format() and % — The Older Styles
Which Style Should You Use?
f-strings (added in Python 3.6) are the modern, idiomatic choice — they're more readable since the variable sits directly inside the string instead of matched up positionally, and they're generally faster. .format() and % both still appear constantly in real-world and older code, so recognizing them matters even though you should default to f-strings in new code you write.
Slicing
s[start:stop:step] — stop is exclusive, negative indices count from the end.
Immutability
String methods never modify in place — they always return a new string.
String Methods
.strip(), .split(), .join(), .replace(), .find() and more.
f-strings
f"{expr:.2f}" — the modern, idiomatic way to build formatted strings.
💻 Coding Challenges
Challenge 1: Reverse Words
Given the string "Python is fun", use .split() and .join() to print the words in reverse order: "fun is Python".
Goal: Practice combining .split() and .join() with slicing to reverse a sequence.
Challenge 2: Formatted Receipt Line
Given item = "Coffee", price = 4.5, and qty = 3, use an f-string to print a line like "3x Coffee: $13.50", with the total formatted to exactly 2 decimal places.
Goal: Practice f-string expressions and the :.2f format spec together.
Challenge 3: Palindrome Check
Write code that checks whether the string "racecar" is a palindrome (reads the same forwards and backwards), using slicing rather than a loop.
Goal: Practice using the [::-1] slice pattern to solve a real small problem.
🎯 What's Next
Next chapter: Lists, Tuples & Sets — mutable vs. immutable sequences, and a first look at comprehensions.