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.
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.
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.
Tracing a score message
Determine what is stored in score and what is stored in message.
- 1Read the variable
score is assigned the integer value 42.
- 2Evaluate the braces
The expression inside {score} produces the current value of score, which is 42.
- 3Build the f-string
The f-string combines the literal text Score: with the string representation of the embedded value.
- 4Separate the results
score remains the raw integer value used by the expression, while message is the complete string produced by the f-string.
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.
name contains "Mina".
items contains 3.
summary contains the string "Mina has 3 items.".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.
score = 42
message = f"Score: {score}"
score = 50
updated_message = f"Score: {score}"- 1score = 42 establishes the value used by the first f-string.
- 2message = f"Score: {score}" evaluates the braces and stores the resulting string.
- 3score = 50 changes the variable used by future expressions.
- 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_messageusing 50.
message is "Score: 42".
updated_message is "Score: 50".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 likescore = 42; message = f"Score: {score}"; score = 50Why it failsThe f-string was evaluated before score changed, so message already contains the string representation created from 42.
Do this insteadEvaluate 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 likescore = 42; message = f"Score: {score}"Why it failsscore is the raw value used by the embedded expression, while message is the string produced by the complete f-string.
Do this insteadTrace the variable and the f-string result separately.
Common mistake Ignoring the boundary between literal text and the expression in braces.
What it looks likef"Score: {score}"Why it failsThe text outside the braces is literal text, while the part inside the braces is evaluated before insertion.
Do this insteadMark each section as either surrounding literal text or an embedded expression.
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
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 = 8Hints
- 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?
Reveal 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
- 1An f-string is a string literal prefixed with f, with expressions embedded inside curly braces.
- 2The complete result of an f-string is always a string, even when an embedded expression produces an integer.
- 3The raw variable value and the string representation inserted into an f-string result should be traced separately.
- 4An f-string is evaluated once, so later changes to a variable do not alter an already-created result.
- 5To 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.