Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is an f-string in Python?
An f-string is a way to embed expressions inside string literals, using curly braces {} and prefixing the string with the letter 'f'. It makes string formatting easier and more readable.
Click to reveal answer
beginner
How do you include a variable named name inside an f-string?
You write the string with an f before the quotes and put the variable inside curly braces like this: f"Hello, {name}!".
Click to reveal answer
intermediate
How can you format a floating-point number to show only 2 decimal places using f-strings?
Use a format specifier inside the braces like this: f"{value:.2f}". This rounds the number to 2 decimal places.
Click to reveal answer
intermediate
Can you use expressions inside f-strings? Give an example.
Yes! You can put any valid Python expression inside the braces. For example: f"5 + 3 = {5 + 3}" will output 5 + 3 = 8.
Click to reveal answer
advanced
How do you include a literal curly brace character in an f-string?
To include a curly brace, double it: use {{ for { and }} for }. For example: f"{{Hello}}" outputs {Hello}.
Click to reveal answer
What prefix do you add before a string to create an f-string in Python?
Af
Bs
Cr
Db
✗ Incorrect
The prefix 'f' before the quotes tells Python to treat the string as an f-string.
How would you format the number 3.14159 to show only 2 decimal places using an f-string?
Af"{3.14159:2d}"
Bf"{3.14159:.2d}"
Cf"{3.14159:.2f}"
Df"{3.14159:2f}"
✗ Incorrect
The format specifier '.2f' rounds the number to 2 decimal places.
What will this code print? name = 'Sam' print(f"Hello, {name}!")
AHello, {name}!
BHello, Sam!
CHello, name!
DError
✗ Incorrect
The variable 'name' is replaced by its value 'Sam' inside the f-string.
How do you include a literal curly brace '{' in an f-string?
AYou cannot include literal braces in f-strings
BUse a backslash before it: \{
CPut it inside quotes: '{'
DDouble the brace: {{
✗ Incorrect
Doubling the brace '{{' or '}}' tells Python to include a literal brace.
Can you put calculations inside the curly braces of an f-string?
AYes, any valid Python expression can be used
BOnly string operations are allowed
COnly simple math operations are allowed
DNo, only variables are allowed
✗ Incorrect
F-strings can evaluate any valid Python expression inside the braces.
Explain how to use f-strings to include variables and expressions inside strings.
Think about how you write the string and where you put the variable names.
You got /4 concepts.
Describe how to format numbers with specific decimal places using f-strings.
Look inside the braces for a colon and formatting code.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using f-strings in Python?
easy
A. To easily insert variables or expressions inside strings
B. To create lists from strings
C. To convert strings to uppercase
D. To repeat strings multiple times
Solution
Step 1: Understand f-string usage
F-strings allow embedding variables or expressions inside strings using curly braces {}.
Step 2: Compare options
Only To easily insert variables or expressions inside strings describes this purpose correctly; others describe unrelated string operations.
Final Answer:
To easily insert variables or expressions inside strings -> Option A
Quick Check:
f-strings = insert variables in strings [OK]
Hint: Remember: f-strings embed variables with curly braces {} [OK]
Common Mistakes:
Confusing f-strings with string methods like upper()
Thinking f-strings create lists
Assuming f-strings repeat strings
2. Which of the following is the correct syntax to use an f-string to insert variable name into a string?
easy
A. "Hello, $name!"
B. f"Hello, {name}!"
C. f'Hello, $name!'
D. "Hello, {name}!"
Solution
Step 1: Identify f-string syntax
An f-string starts with the letter f before the quotes and uses curly braces {} to insert variables.
Step 2: Check each option
f"Hello, {name}!" correctly uses f"Hello, {name}!". Options A and B lack the f prefix, and C uses incorrect syntax with $name.
Final Answer:
f"Hello, {name}!" -> Option B
Quick Check:
f-string = f"text {var}" [OK]
Hint: f-string must start with f and use {variable} inside quotes [OK]
Common Mistakes:
Omitting the f before the string
Using $ instead of curly braces
Using single quotes without f prefix
3. What will be the output of this code?
name = "Alice"
age = 30
print(f"{name} is {age} years old.")
medium
A. name is age years old.
B. {name} is {age} years old.
C. SyntaxError
D. Alice is 30 years old.
Solution
Step 1: Evaluate variables inside f-string
The variables name and age are replaced by their values "Alice" and 30 inside the string.
Step 2: Understand print output
The print statement outputs the string with variables replaced: "Alice is 30 years old.".
Final Answer:
Alice is 30 years old. -> Option D
Quick Check:
f-string replaces {name} and {age} with values [OK]
Hint: f-string prints variable values, not variable names [OK]
Common Mistakes:
Expecting curly braces to print literally
Confusing variable names with values
Thinking f-string causes syntax error here
4. Find the error in this code snippet:
age = 25
print(f"Age is {age")
medium
A. Missing closing brace '}' in f-string
B. Missing f before the string
C. Variable age is not defined
D. Incorrect use of quotes
Solution
Step 1: Check f-string braces
The f-string has an opening brace { but no matching closing brace }, causing a syntax error.
Step 2: Verify other parts
The f prefix is present, variable age is defined, and quotes are correct.
Final Answer:
Missing closing brace '}' in f-string -> Option A
Quick Check:
Every { must have a matching } in f-string [OK]
Hint: Count braces in f-string; each { needs a } [OK]
Common Mistakes:
Forgetting to close curly braces
Thinking missing f causes this error
Assuming variable is undefined
5. You want to print the price of an item with 2 decimal places using f-strings. Which code correctly formats the price variable?
price = 9.8765
# Choose the correct print statement
hard
A. print(f"Price: ${price:2f}")
B. print(f"Price: ${price:.2}")
C. print(f"Price: ${price:.2f}")
D. print(f"Price: ${price:.2d}")
Solution
Step 1: Understand format specifier for decimals
To format a float with 2 decimal places, use :.2f inside the curly braces.
Step 2: Check each option
print(f"Price: ${price:.2f}") uses {price:.2f} correctly. print(f"Price: ${price:2f}") misses the dot, print(f"Price: ${price:.2}") misses the type specifier, and print(f"Price: ${price:.2d}") uses invalid d for decimals.
Final Answer:
print(f"Price: ${price:.2f}") -> Option C
Quick Check:
Use :.2f to format float with 2 decimals [OK]
Hint: Use :.2f inside {} to show 2 decimal places [OK]