Working with Multiple Data Types in Expressions
An f-string embeds expressions inside curly braces within a string literal by prefixing the string with f.
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.
Read the variable: score is assigned the integer value 42.
Evaluate the braces: The expression inside {score} produces the current value of score, which is 42.
Build the f-string: The f-string combines the literal text Score: with the string representation of the embedded value.
Separate 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}"
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.
Assuming the f-string result remains connected to the variable.
The f-string was evaluated before score changed, so message already contains the string representation created from 42.
Fix:
Evaluate another f-string after the variable changes if you need text containing the new value.Treating the embedded integer and the complete result as the same kind of value.
score is the raw value used by the embedded expression, while message is the string produced by the complete f-string.
Fix:
Trace the variable and the f-string result separately.Ignoring the boundary between literal text and the expression in braces.
The text outside the braces is literal text, while the part inside the braces is evaluated before insertion.
Fix:
Mark 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 = 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?
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
- An f-string is a string literal prefixed with f, with expressions embedded inside curly braces.
- The complete result of an f-string is always a string, even when an embedded expression produces an integer.
- The raw variable value and the string representation inserted into an f-string result should be traced separately.
- An f-string is evaluated once, so later changes to a variable do not alter an already-created result.
- To debug an f-string, evaluate each embedded expression first and then combine its representation with the surrounding literal text.
Key Takeaways
- 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.