Home › Interview Questions › How can you extract the domain part from an email …

How can you extract the domain part from an email address in Python?

🟢 Easy Coding Junior level
1Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

To extract the domain part from an email address in Python, split the string at the '@' character to separate the local part from the domain. For example: local, domain = email.split('@', 1). If you need only the top‑level domain (e.g., 'com' from 'example.com'), you can split the domain at the last '.' or use a simple approach: tld = domain.split('.')[-1]. The entire operation is O(n) where n is the length of the email string, because split scans the string once. This method works for standard email formats and is easy to understand. If you need to validate the email format, consider using the email.utils.parseaddr function or a library like validate_email. Example code: def get_domain(email): local, domain = email.split('@', 1); return domain. This returns 'example.com' for 'user@example.com'.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500