0
0
Pythonprogramming~10 mins

Formatting structured data in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Formatting structured data
Start with data structure
Choose format: JSON, CSV, etc.
Use formatting function
Generate formatted string
Output or save formatted data
End
This flow shows how to take a data structure, choose a format, format it, and output the result.
Execution Sample
Python
import json

person = {"name": "Anna", "age": 28, "city": "Paris"}

json_string = json.dumps(person, indent=2)
print(json_string)
This code formats a Python dictionary into a nicely indented JSON string and prints it.
Execution Table
StepActionInputOutput/Result
1Define dictionaryperson = {"name": "Anna", "age": 28, "city": "Paris"}person holds the dictionary
2Call json.dumps with indent=2person dictionaryReturns JSON string formatted with 2 spaces indentation
3Print the JSON stringFormatted JSON string{ "name": "Anna", "age": 28, "city": "Paris" }
4EndN/AProgram ends after printing
💡 All steps complete, JSON string printed with indentation
Variable Tracker
VariableStartAfter json.dumpsFinal
person{"name": "Anna", "age": 28, "city": "Paris"}{"name": "Anna", "age": 28, "city": "Paris"}{"name": "Anna", "age": 28, "city": "Paris"}
json_stringN/A{ "name": "Anna", "age": 28, "city": "Paris" }{ "name": "Anna", "age": 28, "city": "Paris" }
Key Moments - 2 Insights
Why do we use json.dumps instead of print(person)?
print(person) shows the dictionary in Python format without indentation. json.dumps formats it as a JSON string with indentation, making it easier to read, as shown in execution_table step 2 and 3.
What does the indent=2 argument do?
indent=2 tells json.dumps to add line breaks and 2 spaces per indentation level, making the output easier to read. This is shown in the output column of step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 2?
AAn error message
BThe original dictionary printed as is
CA JSON string with indentation
DAn empty string
💡 Hint
Check the Output/Result column in row for step 2 in execution_table
At which step is the formatted JSON string printed to the screen?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the Action column and Output/Result for step 3 in execution_table
If we remove indent=2 from json.dumps, how would the output change?
AOutput would be the same with indentation
BOutput would be a compact JSON string without line breaks
COutput would be a Python dictionary
DProgram would crash
💡 Hint
Think about what indent=2 does as explained in key_moments and execution_table step 2
Concept Snapshot
Formatting structured data means converting data like dictionaries into readable strings.
Use json.dumps() to convert Python dicts to JSON strings.
The indent parameter adds spaces and line breaks for readability.
Without indent, JSON is compact and hard to read.
Formatted data is easier to share and understand.
Full Transcript
This example shows how to format structured data in Python using the json module. We start with a dictionary named person. Using json.dumps with indent=2, we convert the dictionary into a nicely formatted JSON string with line breaks and spaces. Then we print this string. The execution table traces each step: defining the dictionary, formatting it, printing the result, and ending the program. The variable tracker shows how the dictionary stays the same, and the json_string variable holds the formatted JSON after calling json.dumps. Key moments clarify why json.dumps is used and what indent=2 does. The visual quiz tests understanding of the output at each step and the effect of indent. The concept snapshot summarizes the main points about formatting structured data for readability.