Skip to main content
CodeFlow
control flow~12 minRoute 01 3 of 12

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

0H1E2L3L4Olen = 5 · slot 0 = "H"

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

Concatenation glues with +. 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

I'm immutable in most languages — every change builds a fresh copy. That's why string-building inside a loop can quietly become slow.

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

  1. Predict — Question 1

    What does this print in Python?

    s = "abc"
    s = s + "d"
    print(s)
  2. Question 2

    Why is repeatedly building a string with `+=` inside a loop sometimes slow?

  3. True or false — Question 3

    In Python, "abc" == "abc" is True regardless of how the two strings were constructed.

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.