Complete the code to convert the dictionary to a JSON string.
import json person = {"name": "Alice", "age": 30} json_str = json.[1](person) print(json_str)
The json.dumps() function converts a Python object into a JSON string.
Complete the code to pretty-print the JSON string with indentation.
import json data = {"city": "Paris", "population": 2148327} pretty_json = json.dumps(data, [1]=4) print(pretty_json)
The indent parameter adds spaces to format the JSON string nicely.
Fix the error in the code to load JSON data from a string.
import json json_data = '{"fruit": "apple", "count": 10}' data = json.[1](json_data) print(data)
The json.loads() function reads a JSON string and converts it to a Python object.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ["apple", "bat", "cat", "door"] lengths = {word: [1] for word in words if [2] > 3} print(lengths)
The first blank needs len(word) to get the length. The second blank also needs len(word) to check if length is greater than 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their counts only if count is positive.
counts = {"apple": 2, "banana": 0, "cherry": 5}
result = { [1]: [2] for [3], count in counts.items() if count > 0 }
print(result)The key is the uppercase word (word.upper()), the value is the count, and the loop variable is word.