1.2.13 · D1Introduction to Programming (Python)

Foundations — f-strings — embedding expressions

2,500 words11 min readBack to topic

Before you can read a single line of the parent note, you meet a pile of symbols and words: "...", f, { }, =, :, +, str(), .upper(), [0], "expression", "scope". If any of these is a fog to you, the parent note will feel like magic instead of logic. So we take them one at a time, each with a plain meaning, a picture, and the reason the topic needs it.


1. A string — text the computer stores literally

Picture text as beads on a wire — each bead is one character, and the two quote marks are the clips at each end that say "everything between us is just beads, don't try to run it."

Figure — f-strings — embedding expressions

The quotes are not part of the text. "Ada" is the three characters A, d, a. The quotes are just the fence.

See also Strings — literals and quotes.


2. A variable — a labelled box holding a value

Picture a box on a shelf with a sticky label. The label is the name (name, age). The thing inside is the value ("Ada", 36).

Figure — f-strings — embedding expressions

See also Variables and scope.


3. Scope — which boxes are visible right now

Picture a room: the boxes on the shelves in your room are reachable; boxes locked in another room are not. Code inside braces stands in the same room as the code around it.


4. An expression — code that produces a value

This is the most important foundation for the whole topic, so go slow.

Contrast it with a statement, which does something but need not hand you a value.

You write Is it an expression? Value it becomes
7 yes 7
7 + 5 yes 12
name yes "Ada"
word.upper() yes "PYTHON"
word[0] yes "p"
age = 36 no — it's an assignment statement (nothing to hand back)

Picture an expression as a juicer: you feed in code, one drop of juice (the value) comes out the bottom. An assignment is a filing action — it puts a folder away but produces no juice.

Figure — f-strings — embedding expressions

Related tools you'll see used as expressions:

  • str() and type conversionstr(...) is an expression.
  • print() functionprint(...) is where f-strings usually end up.

5. The building-block operators & accessors

These are small expression-makers the parent note drops in without ceremony.

Picture indexing as numbered parking spots along the string, starting at spot 0:

Figure — f-strings — embedding expressions

6. str() and "convert to text"

Picture a printing press: a number 36 goes in, and out comes a printed label reading 36 — same look, different material (text, not number).

See str() and type conversion and the older str.format() method.


7. The f, the { }, and the : — the three new marks

Now that every underlying idea exists, the three symbols the topic adds make sense.

Reading the substitution law (a note on LaTeX notation)

The parent note states the rule as a formula written in LaTeX, the language used to typeset maths on these pages. Two marks look scary but are harmless once named:

  • \text{...} just means "show these words in normal upright letters" (not italic maths letters). So \text{concat} displays the plain word concat.
  • \Big( ... \Big) are simply big round brackets stretched tall so they wrap a long expression — ordinary parentheses, sized up.
  • \dots is the printed form of "...", meaning "and so on, the pattern continues."

With those decoded, here is the law:


8. Two edge cases you'll meet immediately

These are not new foundations, but they are marks that appear inside braces and confuse beginners the first time, so meet them now.


Prerequisite map

The diagram below is written in Mermaid, a tiny text language for drawing flowcharts. Read it like this: each box is a foundation, and an arrow A --> B means "you need A before B makes sense." Follow the arrows upward-to-downward toward the f-string at the bottom.

String = fenced text

f-string

Variable = labelled box

Expression = code with a value

Scope = reachable boxes

Operators + methods + indexing

Braces hold an expression

str = print as text

Substitution: replace expr with its text

f prefix = fill the blanks

colon = format costume

How to read it: start at any top box, follow each arrow — the thing the arrow points at depends on the thing it comes from. Everything eventually flows into "f-string."


Equipment checklist

Cover the right side and check you can answer each before reading the parent note.

What do the quote marks around a string actually do?
They fence off the characters as plain text and are not part of the text themselves.
In age = 36, what does the single = mean?
"Put the value 36 into the box named age" — an assignment action, not a maths equality.
What is a variable?
A name that points at a stored value; using the name fetches whatever is in that box.
What is scope?
The set of variable boxes reachable at the point where a line of code runs.
Give the one-sentence test for whether something is an expression.
Ask "if I ran this, would I be left holding a single value?" — if yes, it's an expression.
Is x = 5 an expression? Why does it matter here?
No — it's an assignment statement with no value; f-string braces need an expression, so {x = 5} errors.
What is word[0] and what does it give for word = "python"?
Indexing at position 0 (Python counts from 0), giving the first character "p".
What does str(36) return, and how is it different from 36?
The text "36" (characters you display), which is no longer a number you can do arithmetic with.
What does the f prefix switch on?
It tells Python to treat { } as fill-in blanks holding expressions, not as literal characters.
What does the : inside a brace introduce?
A format specification — a "costume" describing how to display the value (decimals, commas, padding).
How do you print a literal { inside an f-string?
Double it — write {{ to show one {, and }} to show one }.
What does the !r conversion flag do, as in f"{name!r}"?
It uses repr(value) instead of str(value), so a string is shown with its quotes, e.g. 'Ada'.

Connections

  • 1.2.13 f-strings — embedding expressions (Hinglish) — the parent idea in Hinglish.
  • Strings — literals and quotes — §1, what quotes fence off.
  • Variables and scope — §2, §3, boxes and reachability.
  • str() and type conversion — §6, print-as-text.
  • str.format() method — the older sibling of the substitution rule.
  • Format specification mini-language — §7–§8, what lives after the : and the !r/!s/!a flags.
  • print() function — where f-strings are usually sent.