Module 12: Python Standard LibrariesBeginner20 min read
Working with JSON & Regular Expressions
Serialize/deserialize JSON and match pattern-based strings with the re module.
Key Takeaways & Core Concepts
json.dumps(obj): Convert Python dictionary/list to a JSON string.json.loads(string): Parse JSON string into Python dict/list.re.search(pattern, text): Scan through string looking for pattern.re.findall(pattern, text): Find all matching instances.
Working with JSON & Regular Expressions
### JSON & Regex in Python
#### 1. JSON Module (import json) JSON (JavaScript Object Notation) is the standard format for web data exchange.
json.dumps(obj): Convert Python dictionary/list to a JSON string.json.loads(string): Parse JSON string into Python dict/list.
PYTHON Code Example
1import json
2
3data = {"name": "CodeZov", "courses": ["Python", "C", "Docker"]}
4json_str = json.dumps(data)
5parsed = json.loads(json_str)
6print(parsed["name"])
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
6 lines
#### 2. Regular Expressions (import re) Used for pattern searching, validation, and string replacements:
re.search(pattern, text): Scan through string looking for pattern.re.findall(pattern, text): Find all matching instances.re.sub(pattern, replacement, text): Substitute matched patterns.
Extract Email Addresses with Regex
1import re
2
3text = "Contact support@codezov.com or sales@zov.in for inquiries."
4pattern = r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'
5emails = re.findall(pattern, text)
6print("Found Emails:", emails)
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
6 lines
Note: re.findall extracts every non-overlapping substring that matches the email pattern.
Practice & AssessmentPython Programming Tutorial
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)