Hackernoon logoBeginner Python Projects: Build a Simple Email Slicing Program by@mindninjax

Beginner Python Projects: Build a Simple Email Slicing Program

Author profile picture

@mindninjaxRishabh Singh

LIVE + LOVE + CODE!

Hello everyone, today we will build anĀ Email Slicer in Python.

What is an Email Slicer?

An Email Slicer is just a tool which will take an email id as an input and will perform slicing operations on it to return the username and the domain of the email id.

For example:

Input:

Output:

Your username is rishabh.singh & domain is gmail.com

Here we returnedĀ rishabh.singhĀ as username andĀ gmail.comĀ as a domain.

This project is super simple and quick and it doesn't require any setup so let's quickly jump to coding and build this.

Let's Code

So this first we are going to do is to ask the user to enter the email to be sliced.

email = input("Enter Your Email: ").strip()

Here, as usual, we are making use of theĀ input()Ā function to get the input from the user in the form of string. We will store this input in theĀ emailĀ variable.

Also notice that we are making use of aĀ strip()Ā function.Ā strip()Ā function will remove any additional & unwanted spacing on both sides of strings. So that we can make sure that we have only the email in the input and not any unwanted spaces.

Let's move on to the next step now.

username = email[:email.index('@')]
domain = email[email.index('@') + 1:]

Here, we are slicing the user input to obtain the username and domain and ignore the rest.

Let's see how it works.

In case ofĀ usernameĀ variable we only want to keep the part of the string which comes beforeĀ @Ā and ignore the rest.

Here, we are making use of the slicing operator:Ā andĀ index() function. index() function searches for the particular element or character within the string and lists it is associated with and return its index number.

Let's consider the input isĀ [email protected], so when we write email[:email.index('@')]i, our index() function will interpret it as email[:13] as ourĀ @Ā is located at indexĀ 13. NowĀ email[:13]Ā knows thatĀ @Ā is located at indexĀ 13, so now it will keep the part before indexĀ 13Ā and discard the rest.

This exact same process is followed for theĀ domainĀ also.

And now finally, let's print our output.

print(f"Your username is {username} & domain is {domain}")

We are making use ofĀ f-string, a new addition to Python 3 which allows us to directly place our variables in the output string. Feel free to make use of format()Ā or old schoolĀ +Ā orĀ ,Ā operators if you don't want to useĀ f-string.

Source Code

You can find the complete source code of this project here:

mindninjaX/Python-Projects-for-Beginners

Support

Thank you so much for reading! I hope you found this beginner project useful.

If you like my work please considerĀ Buying me a CoffeeĀ so that I can bring more projects, more articles for you.

Also if you have any questions or doubts feel free to contact me onĀ Twitter, LinkedIn &Ā GitHub. Or you can also post a comment/discussion & I will try my best to help you :D

Also published at https://dev.to/mindninjax/how-to-build-an-email-slicer-using-python-1m4m

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.