Display with Variables
Use f-strings to embed variable values in the option display
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
You now have variables for all four options, but the print() statements still use hardcoded text. The variables aren't being used yet. If you renamed option_1 from "int" to something else, the display wouldn't change.
You need a way to insert a variable's value into a string. That's what f-strings do.
How f-strings work
Prefix a string with f and wrap a variable name in {}. Python replaces {name} with the variable's value when the line runs:
color = "blue"
print(f"The sky is {color}.")Output: The sky is blue.
The f before the opening quote activates the substitution. Without it, {color} is treated as literal text — Python prints the curly braces and all.
Combining a number and a variable
You can mix fixed text and variable references in the same f-string:
rank = 1
label = "gold"
print(f" {rank}) {label}")Output: 1) gold
That's exactly the pattern you'll use for the quiz options.
Instructions
Wire the option variables into the display using f-strings.
- Replace each option print with an f-string version — once updated, changing
option_1 = "int"to any other string will automatically update what the player sees:- Replace
print(" 1) int")withprint(f" 1) {option_1}") - Replace
print(" 2) str")withprint(f" 2) {option_2}") - Replace
print(" 3) float")withprint(f" 3) {option_3}") - Replace
print(" 4) bool")withprint(f" 4) {option_4}")
- Replace
question = "What is the type of the value 3.14?"
option_1 = "int"
option_2 = "str"
option_3 = "float"
option_4 = "bool"
print("=== Python Quiz ===")
print()
print(question)
print()
# Step 1: Replace the four option prints with f-strings
print(" 1) int")
print(" 2) str")
print(" 3) float")
print(" 4) bool")
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding