Editor

Extract a Domain from an Email Address

Beginner · Write the code · from Python Strings: Create, Access, Slice, and Manipulate Text

From the lesson: Python Strings: Create, Access, Slice, and Manipulate Text

Think about your phone. Almost everything you see on the screen is text: names in your contacts, messages from friends, search results, notifications. Behind the scenes, all of that text is handled by strings.

Read the lesson

You are given an email address stored in email. Extract just the domain name (the part after the @ symbol) and print it.

Expected output:

gmail.com

Use the .find() method to locate the @ symbol, then use slicing to extract everything after it.

email = "alice@gmail.com"
# Find the position of the @ symbol
# Slice the string to get the domain
# Print the domain

More Strings exercises

All Python exercises · 50 practice problems · Python Fundamentals quiz