Challenge - 5 Problems
F-string Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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.")
Attempts:
2 left
💡 Hint
Remember that expressions inside curly braces in f-strings are evaluated.
✗ Incorrect
The expression {age + 5} calculates 30 + 5 = 35, so the output is 'Alice will be 35 years old in 5 years.'
❓ Predict Output
intermediate2: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}")
Attempts:
2 left
💡 Hint
The format specifier .2f rounds the float to 2 decimal places.
✗ Incorrect
The format specifier .2f rounds the number 3.14159 to 3.14 in the output.
❓ Predict Output
advanced2: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.")Attempts:
2 left
💡 Hint
You can use dictionary keys inside the curly braces in f-strings.
✗ Incorrect
The f-string evaluates person['name'] as 'Bob' and person['age'] as 25, so it prints 'Bob is 25 years old.'
❓ Predict Output
advanced2: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}.")
Attempts:
2 left
💡 Hint
Expressions inside curly braces are evaluated before printing.
✗ Incorrect
x**2 is 16 and x**3 is 64, so the output is 'Square of 4 is 16 and cube is 64.'
❓ Predict Output
expert2: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}")
Attempts:
2 left
💡 Hint
The >3 means right-align the number in a space of width 3.
✗ Incorrect
Each number is printed right-aligned in a field 3 characters wide, so there are spaces before the number.