Ayushi Rawat's Blog

Ayushi Rawat's Blog

Email Slicer with Python

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!

Interested in reading more such articles from Ayushi Rawat?

Support the author by donating an amount of your choice.

Recent sponsors