Mini Project: Contact Book App
Build a contact book app using dictionaries, string methods, and JSON — combining everything from Module 5.
20 min•By Priygop Team•Updated 2026
Contact Book App
Contact Book App
import json
class ContactBook:
def __init__(self):
self.contacts = {}
def add(self, name, phone, email=""):
self.contacts[name.title()] = {
"phone": phone,
"email": email
}
print(f" Added: {name.title()}")
def search(self, query):
query = query.lower()
results = {k: v for k, v in self.contacts.items()
if query in k.lower() or query in v["phone"]}
return results
def display_all(self):
if not self.contacts:
print(" No contacts found.")
return
print("\n === Contact Book ===")
for name, info in sorted(self.contacts.items()):
print(f" {name}")
print(f" Phone: {info['phone']}")
if info['email']:
print(f" Email: {info['email']}")
def to_json(self):
return json.dumps(self.contacts, indent=2)
# Demo
book = ContactBook()
book.add("Alice Smith", "9876543210", "alice@email.com")
book.add("Bob Jones", "8765432109", "bob@email.com")
book.add("Charlie Brown", "7654321098")
book.add("Alice Wang", "6543210987", "awang@email.com")
book.display_all()
# Search
print("\n Search 'alice':")
results = book.search("alice")
for name, info in results.items():
print(f" {name}: {info['phone']}")
# Export as JSON
print("\n === JSON Export ===")
print(book.to_json())Tip
Tip
Combine dicts, classes, and JSON for real-world data apps. Use __str__ for user output and to_json() for data export.
Diagram
Loading diagram…
Map for dictionaries. Object for records/config. WeakMap for metadata that shouldnt prevent GC.
Common Mistake
Warning
Not handling the case where a search returns empty results. Always check if results exist before iterating.
Practice Task
Note
(1) Add a delete contact feature. (2) Save contacts to a JSON file. (3) Add phone number validation with regex.