Back

Activity 1.6: String Interpolation

divider

Activity 1.6

String Interpolation

Key Concepts

A Cleaner Way to Combine Text and Variables

f"..." Strings

The Old Way

You've been combining variables into a sentence by passing multiple values to print(), separated by commas.

The Old Way

Demo
score = 95
print("You scored", score, "%.")
Demo
You scored 95 %.

Notice the awkward space before %. — commas always insert a space, whether you want one or not.

f-Strings

Place an f right before the opening quote, then wrap a variable name in curly braces { } to insert its value directly into the string.

f-Strings

Demo
score = 95
print(f"You scored {score}%.")
Demo
You scored 95%.

Expressions Inside Braces

You can put more than a plain variable inside { } — a full expression works too, and Python evaluates it before inserting the result.

Expressions Inside Braces

Demo
health = 100
damage = 35
print(f"After the attack, you have {health - damage} HP left.")
Demo
After the attack, you have 65 HP left.

Today's Objectives

  • Writing f-strings
  • Inserting variables into a sentence
  • Embedding expressions directly inside { }

Key Terms

f-String
A string prefixed with f that can insert variables or expressions directly inside it.
Placeholder
The { } spot inside an f-string where a value gets inserted.
String Interpolation
Inserting a variable's value directly into a string.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-6-interpolation.


Task 1: Introduce Yourself

  • Create name and age variables.
  • Print an f-string that introduces yourself using both variables.
String Interpolation
name = "Anthony"
age = 35
print(f"My name is {name} and I am {age} years old.")
#

Task 2: Do Some Math Inline

  • Create score and bonus variables.
  • Print an f-string that adds the two variables together inside the curly braces.
String Interpolation
name = "Anthony"
age = 35
print(f"My name is {name} and I am {age} years old.")
score = 40
bonus = 10
print(f"Your final score is {score + bonus} points.")
#

Task 3: Multiple Placeholders

  • Create favorite_color and favorite_number variables.
  • Print a single f-string that uses both variables in one sentence.
String Interpolation
name = "Anthony"
age = 35
print(f"My name is {name} and I am {age} years old.")
score = 40
bonus = 10
print(f"Your final score is {score + bonus} points.")
favorite_color = "green"
favorite_number = 7
print(f"My favorite color is {favorite_color} and my favorite number is {favorite_number}.")
#

Challenge (Optional): Refactor an Old Line

  • Copy a print() statement from Activity 1.5 and rewrite it as an f-string.
  • Confirm both versions produce the same output.
divider

Checkpoint

Verify your program works correctly.

Example Output
My name is Anthony and I am 35 years old.
Your final score is 50 points.
My favorite color is green and my favorite number is 7.
divider

Reflection

Answer the following questions before submitting your work.

  1. What is one advantage of an f-string over separating values with commas in print()?
  2. What happens when you place an arithmetic expression, like {score + bonus}, inside an f-string's curly braces?
  3. If you completed the Challenge, did rewriting your Activity 1.5 line as an f-string change its output? Why or why not?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete