0
0
Pythonprogramming~20 mins

Formatting structured data in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structured Data Formatter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of JSON formatting with indent
What is the output of the following Python code that formats a dictionary as a JSON string with indentation?
Python
import json

data = {'name': 'Alice', 'age': 30, 'city': 'New York'}
result = json.dumps(data, indent=2)
print(result)
A
{
    "name": "Alice",
    "age": 30,
    "city": "New York"
}
B{"name": "Alice", "age": 30, "city": "New York"}
C
{
"name": "Alice",
"age": 30,
"city": "New York"
}
D
{
  "name": "Alice",
  "age": 30,
  "city": "New York"
}
Attempts:
2 left
💡 Hint
Look at how json.dumps formats the output when indent=2 is used.
Predict Output
intermediate
2:00remaining
Output of pretty-printing nested data
What is the output of this code that pretty-prints a nested dictionary using json.dumps with indent=4?
Python
import json

nested = {'user': {'name': 'Bob', 'details': {'age': 25, 'city': 'Paris'}}}
print(json.dumps(nested, indent=4))
A
{
    "user": {
        "name": "Bob",
        "details": {
            "age": 25,
            "city": "Paris"
        }
    }
}
B{"user": {"name": "Bob", "details": {"age": 25, "city": "Paris"}}}
C
{
  "user": {
    "name": "Bob",
    "details": {
      "age": 25,
      "city": "Paris"
    }
  }
}
D
{
"user": {
"name": "Bob",
"details": {
"age": 25,
"city": "Paris"
}
}
}
Attempts:
2 left
💡 Hint
Indent=4 means four spaces per level of nesting.
Predict Output
advanced
2:00remaining
Output of formatting a list of dictionaries with separators
What is the output of this code that formats a list of dictionaries as JSON with custom separators?
Python
import json

items = [{'id': 1, 'value': 10}, {'id': 2, 'value': 20}]
print(json.dumps(items, separators=(',', ':')))
A[{id:1,value:10},{id:2,value:20}]
B[{"id":1,"value":10},{"id":2,"value":20}]
C[{"id": 1, "value": 10},{"id": 2, "value": 20}]
D[{ "id": 1, "value": 10 }, { "id": 2, "value": 20 }]
Attempts:
2 left
💡 Hint
Separators=(',', ':') remove spaces after commas and colons.
Predict Output
advanced
2:00remaining
Output of sorting keys in JSON output
What is the output of this code that formats a dictionary as JSON with sorted keys?
Python
import json

data = {'b': 2, 'a': 1, 'c': 3}
print(json.dumps(data, sort_keys=True))
A{"c": 3, "b": 2, "a": 1}
B{"b": 2, "a": 1, "c": 3}
C{"a": 1, "b": 2, "c": 3}
D{"a":1,"b":2,"c":3}
Attempts:
2 left
💡 Hint
sort_keys=True orders keys alphabetically.
🧠 Conceptual
expert
2:00remaining
Effect of ensure_ascii=False in JSON formatting
Which option correctly describes the effect of using ensure_ascii=False in json.dumps when formatting data containing non-ASCII characters?
AThe output JSON string contains the original non-ASCII characters instead of escaped Unicode sequences.
BThe output JSON string escapes all characters, including ASCII, into Unicode sequences.
CThe output JSON string raises a UnicodeEncodeError if non-ASCII characters are present.
DThe output JSON string removes all non-ASCII characters from the data.
Attempts:
2 left
💡 Hint
Try printing json.dumps with and without ensure_ascii=False on data with accented letters.