Email Slicer with Python

Subscribe to my newsletter and never miss my upcoming articles

In this Blog article, we will see how to slice any email into username and domain. We will see the implementation in python.

You can refer to my YouTube video tutorial for better Understanding

Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!

Introduction

The email slicer is a quite handy program to get the username and domain name from an email address. You can play around and even customize it further and send a message to the user with this information.

We use make use of email in our day to day life, is one of the most widely used features of the Internet. Email short for electronic mail is a method of exchanging messages between people using electronic devices, along with the web. It allows you to send and receive messages to and from anyone with an email address, anywhere in the world.

E-mail address breakdown:

Screenshot_1.png

  • The first portion of all e-mail addresses, the part before the @ symbol, contains the alias, user, group, or department of a company. In our above example, demoayushi is the username.

  • Next, the @ (at sign) is a divider in the e-mail address. It's required for all SMTP e-mail addresses since Ray Tomlinson sent the first message.

  • Finally, gmail.com is the domain.

Now that you are aware of the basics, its time to code!

Let's get started!

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

carbon (5).png

I am using my demo email address demoayushi@gmail.com. So first, let's input the user's email address.

# Get the user's email address
email = input("What is your email address?: ").strip()

I am using the strip() method to remove spaces at the beginning and at the end of the string if any. The email address is stored in email.

Next, we will slice out the username. It is similar to what do with string slicing.

# Slice out the user name
user_name = email[:email.index("@")]

I am storing the sliced username to user_name. Moving forward let's slice out the domain

# Slice out the domain name
domain_name = email[email.index("@")+1:]

I am storing the sliced domain to `domain_name. Now that we have the domain and username, let's format it.

# Format message
res = "Your username is '{}' and your domain name is '{}'".format(user_name,domain_name)

I am storing the formatted data into res. Once done, we can finally display our result.

# Display the result message
print(res)

I am using the print method to do the same.

#the output
Your username is 'demoayushi' and your domain name is 'gmail.com'

That's it! simple, isn't it?

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

Thank you for reading, I would love to connect with you at Twitter.

Do share your valuable feedback and suggestions!

You should definitely check out my other Blogs:

See you in my next Blog article, Take care!

Shyambhu Mukherjee's photo

Thanks for writing the article. I just wanted to know: why didn't you use python split? Also, can you take more complex cases of emails and try to process them. That would make the article more interesting.

Deactivated User's photo

Here is an error message while running the code... image.png

Show +4 replies
Deactivated User's photo

Hi Anubhav , I installed the latest version also but still that code is not working...

Deactivated User's photo

Hi Anubhav , Got it fixed !!

Deactivated User's photo

Hi Ayushi , Thanks for sharing the knowledge .Its really great. I was running the same code in my jupyter notebook but getting an error message. Could you please let me know if i have to import specific package for email slicing in python as this is super cool and i really want this feature in my python visualization... Thanks again!!