BitBootcampBitBootcamp ← all explainers 🔄 Review ⚙ Settings

PY-01-004-P · Primer — Strings & Formatting

The idea in one sentence

A string is Python's way of holding text — a fixed sequence of characters you can read, slice, and reshape into new strings, and f-strings are the friendly way to weave live values into that text.

Why it exists

Almost everything a program says to a human is text: a greeting, an error message, a file name, a formatted price. Computers store text as strings, and Python gives strings a rich toolkit so you rarely have to fiddle with raw characters yourself. Instead of gluing pieces together by hand, you can ask a string to uppercase itself, strip stray spaces, or swap one word for another. And when you need to drop a name or a number into a sentence, f-strings let you write it the way you'd read it aloud. This is the layer where your code stops being just logic and starts being something people can actually read.

How it works

A string literal is text wrapped in quotes: "Ada" or 'hello' (single and double quotes work the same). Strings are immutable — every method returns a new string and leaves the original untouched. You reach individual characters by indexing with [i] (counting from 0, or from the end with negative numbers), and you grab a range with slicing [start:stop], where stop is excluded. Methods like .upper() and .replace() do the common chores. To build a sentence from live values, put an f before the opening quote and drop expressions inside { } — you can even format numbers, like {score:.1f} for one decimal place.

greeting = "Hello, Bootcamp!"
print(greeting[7])                            # B
print(greeting[7:16])                         # Bootcamp!
print(greeting[-1])                           # !
print(greeting.upper())                       # HELLO, BOOTCAMP!
print(greeting.replace("Hello", "Welcome"))   # Welcome, Bootcamp!

name = "Ada"
score = 91.5
print(f"{name} scored {score:.1f}%")          # Ada scored 91.5%

The one gotcha

Because strings are immutable, greeting.upper() does not change greeting — it hands you back a brand-new string. If you don't capture that result (for example, loud = greeting.upper()), the uppercase version simply vanishes. New learners often write greeting.replace("Hello", "Hi") on its own line, then wonder why nothing changed. Nothing changed because nothing can change: you have to assign the return value to keep it.

See it — how a string method flows

flowchart TD
    A["greeting =<br/>'Hello, Bootcamp!'"] --> B["call<br/>greeting.upper()"]
    B --> C["Python builds a<br/>NEW string"]
    C --> D["'HELLO, BOOTCAMP!'<br/>is returned"]
    D --> E{"Did you assign<br/>the result?"}
    E -->|"loud = ...<br/>(yes)"| F["loud holds the<br/>uppercase text"]
    E -->|"no"| G["result is lost;<br/>greeting unchanged"]
    A --> H["original greeting<br/>stays untouched"]

The one-line takeaway

Strings never change in place — you index and slice to read them, call methods to reshape them into new strings, and reach for f-strings whenever you need to fold live values into text.

Where you'll meet it

Every time you print a report, log what your program did, build a file path, show a price, or greet a user by name, you're formatting strings. The moment you write your first print(f"...") and see a real value slot into a sentence, this clicks — and you'll use it in nearly every program you write from here on.

Named concepts: string literals and immutability, indexing and slicing, common string methods, f-strings for formatting