Bird
0
0

Given a dictionary data = {'x': 10, 'y': 20}, how do you format a string to show "x=10, y=20" using format()?

hard📝 Application Q9 of 15
Python - Input and Output

Given a dictionary data = {'x': 10, 'y': 20}, how do you format a string to show "x=10, y=20" using format()?

A"x={x}, y={y}".format(data)
B"x={0[x]}, y={1[y]}".format(data)
C"x={0}, y={1}".format(data['x'])
D"x={x}, y={y}".format(**data)
Step-by-Step Solution
Solution:
  1. Step 1: Understand dictionary unpacking in format()

    Using **data passes keys as named arguments to format().
  2. Step 2: Use named placeholders matching dictionary keys

    "x={x}, y={y}" matches keys 'x' and 'y'.
  3. Final Answer:

    "x={x}, y={y}".format(**data) -> Option D
  4. Quick Check:

    Use **dict to unpack keys as named args [OK]
Quick Trick: Use **dict to unpack keys for named placeholders [OK]
Common Mistakes:
MISTAKES
  • Passing dict as positional arg
  • Not unpacking dict
  • Using wrong placeholder syntax for dict

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes