Concepts / Introduction to Formatted String Literals

Introduction to Formatted String Literals

Multiple expressions can be embedded in a single f-string by placing each one inside curly braces; Python evaluates them from left to right and inserts each result at its position.

  • Programming

One Sentence, Several Values

A single variable rarely tells a complete story. A report might need a species, a count, and a time period together. A formatted string literal, commonly called an f-string, lets you weave several values into one sentence by placing each expression inside curly braces.

Definition

An f-string embeds expressions inside curly braces within a string literal by prefixing the string with f. Multiple expressions can appear in the same f-string.

The Left-to-Right Trace

Python reads a multi-expression f-string from left to right. Text outside the curly braces remains as written. When Python reaches a pair of braces, it evaluates the expression inside, converts the result to a string, and inserts that result at the braces' position. It then continues through the rest of the string.

f-stringspecies, count, yearsspeciescamelscount0.1years3final stringvalues in originalpositionsfirst expressionthentheninsert each result
How does Python move through each expression and place its evaluated result into the final string?

Building a Multi-Expression Report

Worked example

Three values in one sentence

Use the values species = 'camels', years = 3, and count = 0.1 in one f-string. Place the species first, the count second, and the number of years last.

  1. 1
    Assign the values

    The variables hold the values species = 'camels', years = 3, and count = 0.1.

  2. 2
    Place the expressions

    Write the expressions in the desired output order: species, count, then years.

  3. 3
    Evaluate from left to right

    Python evaluates {species}, then {count}, then {years}, converting each result to its string form and inserting it at that position.

Result

The final text contains camels, 0.1, and 3 in the positions selected by the f-string.

python
Output
We observed camels, with a count of 0.1, over 3 years.
speciescamelscount0.1years3reportone stringinsert at first braceinsert at second braceinsert at third brace
What value does each variable contribute, and what does the complete output look like after all substitutions?

Position Changes the Meaning

The variables themselves do not move when you rearrange an f-string. Only their positions in the resulting sentence change. Python evaluates expressions in the order they appear in the string, not in the order the variables were originally defined.

python
Output
Count: 0.1; species: camels; years: 3.
PartWhat it contributes
VariableThe value currently stored in that variable
Curly-brace expressionThe location where that value is inserted
Text outside bracesStatic text that remains as written
Completed f-stringA string containing the inserted representations

Separate the stored values, their positions, and the completed string.

Numbers and Their String Forms

A variable can hold an integer value such as 3. When that variable appears inside an f-string, Python converts the evaluated result to a string and inserts that string into the surrounding text. The original variable remains separate from the completed f-string result: the variable stores its value, while the f-string result is a string containing that value's representation.

python
years3messageThe observation lasted 3years.converted and inserted
How does an integer remain a numeric value in a variable while becoming text when inserted into an f-string?

Debugging Unexpected Output

When a multi-expression f-string does not say what you intended, debug both the values and the layout. First verify each variable's value individually. Then walk through the f-string from left to right, replacing each brace expression with the value it contributes. This identifies whether the problem is an incorrect value, a misplaced expression, or a missing f prefix.

generated outputCount: camels; species: 0.1intended outputCount: 0.1; species: camelscheck expression order
At which expression, value, or position does the generated output diverge from the intended text?
  • Common mistake

    Leaving out the f prefix

    What it looks like
    "We observed {species}."
    Why it fails

    The string is not marked as an f-string, so the expression is not embedded as intended.

    Do this instead

    Use f before the opening quote: f"We observed {species}."

  • Common mistake

    Using an undefined variable

    What it looks like

    f"We observed {species}." before species has been defined

    Why it fails

    The referenced variable must be defined before the f-string is evaluated.

    Do this instead

    Assign species before creating or evaluating the f-string.

  • Common mistake

    Putting expressions in the wrong order

    What it looks like
    f"Count: {species}; species: {count}."
    Why it fails

    Each value is inserted at the position of its expression, so the labels and values no longer match the intended meaning.

    Do this instead

    Trace the string from left to right and place each expression after the text that describes it.

  • Common mistake

    Assuming the f-string updates later

    What it looks like

    Create a formatted result, then change a variable and expect the old result to change

    Why it fails

    F-strings are evaluated once and store the resulting string representation.

    Do this instead

    Create a new f-string after the variable changes if you need newly evaluated content.

Best practice

For a reliable debugging pass, write down the actual value of each referenced variable, mark the expressions in their left-to-right order, and substitute those values into the surrounding text. This makes the exact point of divergence visible.

Predict Before Running

What do you think happens?

What output does this code produce?

species = "camels"
years = 3
count = 0.1
print(f"{years} years: {count} {species}.")
  • 3 years: 0.1 camels.

  • camels years: 0.1 3.

  • 3 years: camels 0.1.

Reveal answer
Answer:

3 years: 0.1 camels.

Python reads the expressions from left to right. It inserts years first, count second, and species third, while preserving the surrounding text.

EASY

Write an f-string that produces this sentence using the variables species, count, and years: Observed camels: 0.1 over 3 years. Then trace the three expressions from left to right and explain what each contributes.

Hints
  • Begin the string with the exact static text Observed.
  • Place species after the first space, count after the colon and space, and years before the final words.
  • Check that species, count, and years are defined before the f-string is evaluated.

Key Takeaways

  1. 1
    Prefix a string literal with f and place each embedded expression inside curly braces.
  2. 2
    Python evaluates the expressions from left to right and inserts each result at its original position.
  3. 3
    The text outside the braces stays as written, while each expression contributes its evaluated string representation.
  4. 4
    The variable's stored value and the completed f-string result are distinct: the former remains a value, while the latter is a string.
  5. 5
    Debug unexpected output by checking variable values first and then tracing expression order.

At a glance

  • An f-string combines static text with one or more expressions inside curly braces.
  • Expressions are evaluated from left to right, and each result appears where its braces were written.
  • An integer such as 3 can remain stored in a variable while its string representation appears inside an f-string result.
  • Correct debugging requires checking both the values and the order of expressions.
  • An already-created f-string result does not change when its original variables later change.

Related concepts

More concepts starting with I