Bird
Raised Fist0

How do you convert a Python dictionary data into a JSON string with an indentation level of 4 spaces using json.dumps()?

easy📝 Syntax Q3 of Q15
Python - Structured Data Files
How do you convert a Python dictionary data into a JSON string with an indentation level of 4 spaces using json.dumps()?
Ajson.dumps(data, indent='4')
Bjson.dumps(data, indent=4)
Cjson.dumps(data, indent=True)
Djson.dumps(data, indent=None)
Step-by-Step Solution
Solution:
  1. Step 1: Use json.dumps()

    This function converts a Python object into a JSON string.
  2. Step 2: Set indent parameter

    Passing indent=4 formats the JSON string with 4 spaces indentation.
  3. Final Answer:

    json.dumps(data, indent=4) -> Option B
  4. Quick Check:

    Indent must be an integer, not a string or boolean. [OK]
Quick Trick: Indent must be an integer for pretty JSON output. [OK]
Common Mistakes:
MISTAKES
  • Passing indent as a string instead of an integer
  • Using boolean values for indent
  • Omitting the indent parameter for pretty formatting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes