Challenge - 5 Problems
Format Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of positional and keyword arguments in format()
What is the output of this code snippet?
Python
result = "{1} scored {score} points".format("Alice", "Bob", score=95) print(result)
Attempts:
2 left
💡 Hint
Remember that numbers inside curly braces refer to positional arguments by index.
✗ Incorrect
The {1} refers to the second positional argument, which is 'Bob'. The keyword argument 'score' is 95.
❓ Predict Output
intermediate2:00remaining
Using format() with alignment and width
What will this code print?
Python
print("|{:<10}|{:^10}|{:>10}|".format('left', 'center', 'right'))
Attempts:
2 left
💡 Hint
Check the alignment symbols: < for left, ^ for center, > for right.
✗ Incorrect
The first field is left aligned in 10 spaces, second centered, third right aligned.
🧠 Conceptual
advanced2:00remaining
Understanding nested replacement fields
What is the output of this code?
Python
data = {'name': 'John', 'age': 30}
print("{0[name]} is {0[age]} years old".format(data))Attempts:
2 left
💡 Hint
The 0 refers to the first argument, which is a dictionary.
✗ Incorrect
The format method accesses dictionary keys inside the replacement fields.
❓ Predict Output
advanced2:00remaining
Formatting floats with precision and padding
What does this code print?
Python
print("{num:0>8.3f}".format(num=12.34567))
Attempts:
2 left
💡 Hint
0>8 means pad with zeros on the left to width 8, .3f means 3 decimal places.
✗ Incorrect
The number is rounded to 3 decimals and padded with zeros on the left to fill 8 characters.
❓ Predict Output
expert2:30remaining
Using format() with dynamic field names and expressions
What is the output of this code?
Python
person = {'first': 'Jane', 'last': 'Doe'}
index = 'first'
print("{0[{1}]} {0[last]}".format(person, index))Attempts:
2 left
💡 Hint
The second argument is used as a key to access the dictionary in the first argument.
✗ Incorrect
The {0[{1}]} accesses person[index], which is person['first'] = 'Jane'.