DEV Community

Discussion on: How to build an Email Slicer using Python

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.