Email Slicer with Python

Search for a command to run...

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.
Here is an error message while running the code...

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.
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..
email = 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)
print(res)
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)
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

Hi Anubhav , I installed the latest version also but still that code is not working...
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!!
The series content will mostly cover Technical Python tutorials and my recent learnings and experience in Python.
In this Blog article, we will see how to download YouTube video. We will see the implementation in python. Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same! You can refer to my...
All you need to know about Hacktoberfest 2021

Hello reader! Well, Hacktoberfest is something, you are gonna hear a lot about in the coming month. You can refer to my YouTube video Tutorial to see a working tutorial for better understanding and a step-by-step guide of the same. https://www.yo...

Hello reader! In this Blog post, I will share some useful code snippets and functions in Python. Give it a read. Let's get started! 1. Memory The method getsizeof can be used to retrieve the size of any object. Here's an example of the same. import...

Hello reader! Whenever a client sends a request to the server, the response always contains a status code. You might not see it always but it's returned at every client-server interaction. Well, even if you are not a programmer, you would still know...

Hello reader! Microsoft subsidiary GitHub launched Copilot to power pair programming with AI. In this blog post, I will share all you need to know about it. You can refer to my YouTube video Tutorial to see a working tutorial for better understandin...

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

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!
You can find all the code at my GitHub Repository. Drop a star if you find it useful.

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!