Concepts / Working with Multiple Data Types in Expressions

Working with Multiple Data Types in Expressions

The curly braces do not make the entire f-string an integer or another raw value. They identify expressions whose values are inserted into a string result.

  • Programming

One Value, Two Results

An integer can remain an integer in a variable while also appearing as characters inside a string produced by an f-string. The important distinction is not only the value you start with, but also the expression that creates the result. An f-string combines literal text with embedded expressions, and its complete result is always a string.

python
Output
score contains the integer value 42.
message contains the string "Score: 42".

Tracing the Conversion

To trace an f-string, separate the process into three stages. First, identify the expressions inside curly braces. Next, evaluate those expressions using their current values. Finally, place their string representations into the surrounding literal text. The result is a new string value; it is not a live connection to the variables that supplied the values.

score42{score}evaluated value: 4242string formScore: 42complete stringread valuebecomes textjoins literal text
How does the integer stored in a variable become characters in the final string?
Worked example

Tracing a score message

Determine what is stored in score and what is stored in message.

  1. 1
    Read the variable

    score is assigned the integer value 42.

  2. 2
    Evaluate the braces

    The expression inside {score} produces the current value of score, which is 42.

  3. 3
    Build the f-string

    The f-string combines the literal text Score: with the string representation of the embedded value.

  4. 4
    Separate the results

    score remains the raw integer value used by the expression, while message is the complete string produced by the f-string.

Result

score stores 42 as an integer in this example. message stores the string "Score: 42".

Reading F-String Syntax

An f-string is a string literal prefixed with f. Literal text remains outside the curly braces. Each expression inside curly braces is evaluated and inserted into the surrounding text. The braces therefore mark the part that supplies a value, while the rest of the literal supplies the fixed text around it.

fmarks an f-string"starts literalScore:literal text{score}embedded expression"ends literalmarkscontainssurroundsends before
What does each part of an f-string mean, and how does the embedded expression connect to the surrounding literal?
python
Output
name contains "Mina".
items contains 3.
summary contains the string "Mina has 3 items.".
Key point

The curly braces do not make the entire f-string an integer or another raw value. They identify expressions whose values are inserted into a string result.

When Values Change

An f-string is evaluated once when the f-string expression runs. After that evaluation, the resulting string contains the string representation created at that moment. If the original variable changes later, the already-created string does not change with it. To produce text using the new value, evaluate another f-string.

Code walkthrough
score = 42
message = f"Score: {score}"
score = 50
updated_message = f"Score: {score}"
  1. 1
    score = 42 establishes the value used by the first f-string.
  2. 2
    message = f"Score: {score}" evaluates the braces and stores the resulting string.
  3. 3
    score = 50 changes the variable used by future expressions.
  4. 4
    updated_message = f"Score: {score}" evaluates a new f-string using the new value.
  • The first assignment gives score the value 42.
  • The first f-string uses the current value of score and creates message.
  • Changing score to 50 does not revise the already-created message.
  • The second f-string is evaluated after the change, so it creates updated_message using 50.
Output
message is "Score: 42".
updated_message is "Score: 50".
score42score50messageScore: 42messageScore: 42updated_messageScore: 50first evaluationdoes not rewritenew evaluation
What changes when the variable changes after an f-string has already been evaluated?

Debugging the Transformation

When an f-string produces an unexpected result, trace each layer instead of treating the complete expression as one indivisible value. Identify the raw variable value, evaluate each expression inside curly braces, then read the literal text and the inserted representations together. This method makes it easier to see whether the problem is in the value, the embedded expression, or the surrounding text.

  • Common mistake

    Assuming the f-string result remains connected to the variable.

    What it looks like
    score = 42; message = f"Score: {score}"; score = 50
    Why it fails

    The f-string was evaluated before score changed, so message already contains the string representation created from 42.

    Do this instead

    Evaluate another f-string after the variable changes if you need text containing the new value.

  • Common mistake

    Treating the embedded integer and the complete result as the same kind of value.

    What it looks like
    score = 42; message = f"Score: {score}"
    Why it fails

    score is the raw value used by the embedded expression, while message is the string produced by the complete f-string.

    Do this instead

    Trace the variable and the f-string result separately.

  • Common mistake

    Ignoring the boundary between literal text and the expression in braces.

    What it looks like
    f"Score: {score}"
    Why it fails

    The text outside the braces is literal text, while the part inside the braces is evaluated before insertion.

    Do this instead

    Mark each section as either surrounding literal text or an embedded expression.

count3{count}evaluates to 33string representationItems: 3f-string resultevaluaterepresentinsert
What value is evaluated inside the curly braces before the complete string is produced?
Best practice

During debugging, write down the value of each variable before reading the f-string. Then inspect each pair of curly braces separately and finally assemble the surrounding literal text with the resulting representations.

Check Your Trace

EASY

Trace the values in this example. What is stored in level, and what string is stored in label after the code runs?

level = 7
label = f"Level: {level}"
level = 8
Hints
  • Find the value of level when the f-string is evaluated.
  • Separate the raw variable from the string produced by label.
  • The later assignment changes level, not the already-created label.

What do you think happens?

After the practice code runs, what does label contain?

  • "Level: 7"

  • "Level: 8"

  • The integer 7

  • A live reference to level

Reveal answer
Answer:

"Level: 7"

The f-string is evaluated while level is 7. Changing level afterward does not change the string already stored in label.

Key Takeaways

  1. 1
    An f-string is a string literal prefixed with f, with expressions embedded inside curly braces.
  2. 2
    The complete result of an f-string is always a string, even when an embedded expression produces an integer.
  3. 3
    The raw variable value and the string representation inserted into an f-string result should be traced separately.
  4. 4
    An f-string is evaluated once, so later changes to a variable do not alter an already-created result.
  5. 5
    To debug an f-string, evaluate each embedded expression first and then combine its representation with the surrounding literal text.

At a glance

  • F-strings combine literal text with expressions inside curly braces.
  • The result of an f-string is always a string, even when it embeds an integer.
  • A variable keeps its raw value separately from the string representation inserted into an f-string.
  • F-strings are evaluated once and do not update automatically when their source variables change.
  • Debugging works best when you trace each embedded expression before reading the complete string.

More concepts starting with W