Skip to main content

Command Palette

Search for a command to run...

Email Slicer with Python

Published
5 min read
Email Slicer with Python
A

Software Engineer | Technical Blogger | YouTuber | Exploring and Refactoring! | connect with me on Twitter

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!

S

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.

1
D

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

A

Hi Abhishek can you please share the whole code you have written from the above code it looks like you have not defined some variable.

D

Hi Anubhav , Here is the code which is copied from the blog itself.. I think if we are using some different IDE then we need to import package..

Get the user's email address

email = input("What is your email address?: ").strip()

Slice out the user name

user_name = email[:email.index("@")]

Slice out the domain name

domain_name = email[email.index("@")+1:]

Format message

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

Display the result message

print(res)

A

Abhishek Mani Tripathi
I think you are using python2.7, instead of input() us raw_input() and perform other operation. Here goes the code:

email = raw_input("What is your email address?: ").strip()

user_name = email[:email.index("@")]

domain_name = email[email.index("@")+1:]

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

D

Hi Anubhav Thanks but still i am getting the same error message . I have installed Anaconda software in my laptop. Should i install the Python 3.8 and run the code again

image.png

D

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

D

Hi Anubhav , Got it fixed !!

1
D

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!!

More from this blog

A

Ayushi Rawat - The official blog of Ayushi Rawat

72 posts

Software Engineer | Technical Blogger | YouTuber | Exploring and Refactoring! | Connect with me on Twitter