DEV Community

loading...
Cover image for How to build an Email Slicer using Python

How to build an Email Slicer using Python

mindninjax profile image Rishabh Singh ⚡ ・3 min read

Hello everyone, today we will build an Email Slicer.

Alt Text

What is an Email Slicer?

Email Slicer is nothing but 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:

rishabh.singh@gmail.com
Enter fullscreen mode Exit fullscreen mode

Output:

Your username is rishabh.singh & domain is gmail.com
Enter fullscreen mode Exit fullscreen mode

Here we got 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()
Enter fullscreen mode Exit fullscreen mode

Here, as usual, we are making use of 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 to the next step now.

username = email[:email.index('@')]
domain = email[email.index('@') + 1:]
Enter fullscreen mode Exit fullscreen mode

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 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 rishabh.singh@gmail.com, 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 domain also.

And now finally, let's print our output.

print(f"Your username is {username} & domain is {domain}")
Enter fullscreen mode Exit fullscreen mode

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.

https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png

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

Discussion (3)

pic
Editor guide
Collapse
wearypossum4770 profile image
Stephen Smith

Well, I can say this is a great idea. I would like to add a bit of extra input on this post. The string method

partition(match_item)

returns a tuple of the input before match_item, the match_item and the string after the match_item. You have a great ability to anticipate user input, which will help with preventing errors and exceptions later in the code.

So what i can say is this can be done in two lines of code. The first line is tuple unpacking. The next line is what you would like returned.

username, _, domain = input("Enter Your Email: ").strip().partition("@")
print(f"Your username is {username} & domain is {domain}")
Enter fullscreen mode Exit fullscreen mode

A more "pythonic" way of doing it is by using functions. Having the variables in the global space in a module can cause errors.

def email_slicer(email):
    username, _, domain = email.strip().partition("@")
    return f"Your username is {username} & domain is {domain}"
user_input = input("Enter Your Email: ")
print(email_slicer(user_input))
Enter fullscreen mode Exit fullscreen mode

The next step up would be to add some test to your module. Overall pretty good tutorial.

Collapse
amal profile image
Collapse
mindninjax profile image
Rishabh Singh ⚡ Author

Thanks Amal for sharing the alternative method... Highly appreciate it ✨🔥