Strings
Text is a sequence of characters — and almost every language has its own quirks about it.
Prerequisites:VariablesData Types
Story — text is just a row of characters
A string looks like one thing — "hello" — but inside, the computer sees a row of slots, each holding one character. Slot 0 holds h, slot 1 holds e, and so on. Same indexed-row idea as an array — the values just happen to be characters.
Strings are also immutable in most modern languages. Once made, you can't change a character in place; every "edit" builds a fresh string. That sounds wasteful and is sometimes a real performance trap — but it's the property that makes strings safe to share between threads and use as dictionary keys.
Wordy says
+. Interpolation embeds variables right in the literal. Both produce the same string — interpolation is easier to read once you have more than one variable.See it — index, slice, transform
Most useful operations on a string fall into a small set: index (one character), slice (a range), length, contains, upper/lower, split/join, replace, format. Master these eight and you'll handle 90% of text problems.
✎ Sharpen your pencil
In one sentence, what is the difference between concatenation and interpolation?
Wordy says
Try it — predict the slice
Exercise. In Python, s = "codeflow". What is s[0:4]? What is s[-4:]? What does s.replace("flow", "wave") return?
Reveal answers
s[0:4] = "code" (slots 0,1,2,3 — end is exclusive). s[-4:] = "flow" (last 4 — negative indexes count from the end). s.replace(...) returns "codewave" — note "returns": the original s is unchanged because strings are immutable.
Code it — five languages, one row of characters
The core operations have nearly the same names everywhere — only the syntax differs. Watch for length: Go reports bytes, while most others report characters.
name = "Ada"
greeting = "Hello, " + name + "!" # concatenation
greeting2 = f"Hello, {name}!" # interpolation
print(len(name)) # 3
print(name.upper()) # "ADA"
print(name[0]) # "A"Quiz it — make it stick
No Dumb Questions
Real questions other learners asked on this page.
Why are strings immutable?
It makes them safe to share between threads and use as hash-table keys (the hash never goes stale). The cost is that "edits" are really new strings — for heavy editing, use a StringBuilder, list of chars, or bytes buffer.What is a Unicode code point?
A number assigned to each character in the world's writing systems. "A" is U+0041, "🐱" is U+1F431. Modern languages count code points, not bytes — but encoding (UTF-8, UTF-16) is what gets stored on disk.Why does len("café") sometimes give 4 and sometimes 5?
Depends on whether the runtime counts characters (4) or UTF-8 bytes (5 — é is two bytes). Python 3 counts characters. Older languages and lower-level APIs count bytes.