Bird
Raised Fist0
Pythonprogramming~20 mins

String formatting using f-strings in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
F-string Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of f-string with expressions
What is the output of this Python code?
Python
name = "Alice"
age = 30
print(f"{name} will be {age + 5} years old in 5 years.")
AAlice will be 35 years old in 5 years.
BAlice will be age + 5 years old in 5 years.
CAlice will be 30 + 5 years old in 5 years.
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that expressions inside curly braces in f-strings are evaluated.
Predict Output
intermediate
2:00remaining
Formatting numbers with f-strings
What is the output of this code?
Python
value = 3.14159
print(f"Value rounded to 2 decimals: {value:.2f}")
AValue rounded to 2 decimals: 3.14159
BValue rounded to 2 decimals: 3.14
CValue rounded to 2 decimals: 3.1
DValue rounded to 2 decimals: 3.15
Attempts:
2 left
💡 Hint
The format specifier .2f rounds the float to 2 decimal places.
Predict Output
advanced
2:00remaining
Using f-string with dictionary values
What will this code print?
Python
person = {"name": "Bob", "age": 25}
print(f"{person['name']} is {person['age']} years old.")
ABob is 25 years old.
B{person['name']} is {person['age']} years old.
CSyntaxError
DBob is person['age'] years old.
Attempts:
2 left
💡 Hint
You can use dictionary keys inside the curly braces in f-strings.
Predict Output
advanced
2:00remaining
Nested f-string expressions
What is the output of this code?
Python
x = 4
print(f"Square of {x} is {x**2} and cube is {x**3}.")
ASquare of 4 is 8 and cube is 12.
BSquare of x is x**2 and cube is x**3.
CSyntaxError
DSquare of 4 is 16 and cube is 64.
Attempts:
2 left
💡 Hint
Expressions inside curly braces are evaluated before printing.
Predict Output
expert
2:00remaining
Using f-string with alignment and padding
What is the output of this code?
Python
for i in range(1,4):
    print(f"Number: {i:>3}")
A
Number:1
Number:2
Number:3
B
Number: 1
Number: 2
Number: 3
C
Number:  1
Number:  2
Number:  3
DSyntaxError
Attempts:
2 left
💡 Hint
The >3 means right-align the number in a space of width 3.

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

  1. Step 1: Understand f-string usage

    F-strings allow embedding variables or expressions inside strings using curly braces {}.
  2. Step 2: Compare options

    Only To easily insert variables or expressions inside strings describes this purpose correctly; others describe unrelated string operations.
  3. Final Answer:

    To easily insert variables or expressions inside strings -> Option A
  4. 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

  1. Step 1: Identify f-string syntax

    An f-string starts with the letter f before the quotes and uses curly braces {} to insert variables.
  2. 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.
  3. Final Answer:

    f"Hello, {name}!" -> Option B
  4. 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

  1. Step 1: Evaluate variables inside f-string

    The variables name and age are replaced by their values "Alice" and 30 inside the string.
  2. Step 2: Understand print output

    The print statement outputs the string with variables replaced: "Alice is 30 years old.".
  3. Final Answer:

    Alice is 30 years old. -> Option D
  4. 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

  1. Step 1: Check f-string braces

    The f-string has an opening brace { but no matching closing brace }, causing a syntax error.
  2. Step 2: Verify other parts

    The f prefix is present, variable age is defined, and quotes are correct.
  3. Final Answer:

    Missing closing brace '}' in f-string -> Option A
  4. 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

  1. Step 1: Understand format specifier for decimals

    To format a float with 2 decimal places, use :.2f inside the curly braces.
  2. 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.
  3. Final Answer:

    print(f"Price: ${price:.2f}") -> Option C
  4. Quick Check:

    Use :.2f to format float with 2 decimals [OK]
Hint: Use :.2f inside {} to show 2 decimal places [OK]
Common Mistakes:
  • Omitting the dot before precision
  • Using integer format 'd' for floats
  • Leaving out the type specifier