Python Forum
Slicing and printing two halfs of an email address (Udemy)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Slicing and printing two halfs of an email address (Udemy)
#1
I’ve written a script which slices and prints the first half of an email address (before the ‘@’ symbol) and then which also slices and prints the domain name (after the ‘@’ symbol). Here is my basic script so far:

#!/usr/bin/env python3

email_address = "[email protected]"
print(f'Here is your email: {email_address}')
first_half = email_address[0:13]
print(f'Here is your email user name: {first_half}')
second_half = email_address[-11:]
print(f'Here is your email domain name: {second_half}')
Here is the output:

Quote:$ python script1.py
Here is your email: [email protected]
Here is your email user name: Yoseph.Stalin
Here is your email domain name: hotmail.com

It achieves what I set out to accomplish.

But what if I wanted to make it dynamic - - so that the script prompts the user to enter his or her email address and then dynamically discerns before and after the ‘@’ symbol and then prints the user name and domain name (of different lengths) - - how might you people recommend I go about that?

Or to put it another way: how would I tell Python to process an email address so that it will be able to evaluate both sides of the '@' symbol and distinguish domain names of different lengths without the developer having to manually count the length of characters for each set email address?

For my future reference, I got this question from a non-credit Udemy course. This question came up as part of the string module/section. The course is titled "The Python Bible: Everything You Need to Program in Python".



Edit 1: Wow I think just answered my own question. I found a guide which shows how to perform this dynamic operation in 5 different programming languages like Java, C#, and Python.

Python is by far the most elegant. Here is how to extract the domain of an email in Python:

email = '[email protected]'
domain = email.split('@')[1]
I suppose extracting the username would be

email = '[email protected]'
username = email.split('@')[0]
I’ll play around with this today.



Edit 2: Here is my script now:

#!/usr/bin/env python3

email_address = input("Please enter a fake email address and be sure to include an @ symbol: >>  ")
user_name = email_address.split('@')[1]
domain_name = email_address.split('@')[0]
print(f'Here is your email: {email_address}')
print(f'Here is your email user name: {user_name}')
print(f'Here is your email domain name: {domain_name}')
It runs exactly as intended.
Reply
#2
Hi!

Yeah, I like the simplicity of Python!

Just I noticed that in your Edit 2, I think you mistyped the numbers. I think you meant as in Edit 1, [0] for the user_name, and [1] for the domain_name.
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#3
Also think of that is possible to unpack data.
>>> email = '[email protected]'
>>> user_name, domain = email.split('@')
>>> print(f'Your username is <{user_name}> domain is <{domain}>')
Your username is <user> domain is <example.com>
Reply
#4
Another option would be using string.index() function that you are already aware of in another thread.
Reply
#5
Modified solution for cases email address is in form of [email protected] (requires 3.6 <= Python due to f-strings):

>>> email = '[email protected]'
>>> name, domain = email.split('@')
>>> print(f"Hello {' '.join(name.split('.')).title()} from {domain}")
Hello First Last from example.com
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(Aug-23-2019, 11:46 AM)ThomasL Wrote: Another option would be using string.index() function that you are already aware of in another thread.

It's more relevant because it is exactly slicing. str.split() is something different.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,388 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Extracing unique email address from a folder of emails jehoshua 6 2,677 Oct-14-2020, 12:43 AM
Last Post: jehoshua
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 3,725 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  An email with inline jpg cannot be read by all email clients fpiraneo 4 3,932 Feb-25-2018, 07:17 PM
Last Post: fpiraneo
  Email - Send email using Windows Live Mail cyberzen 2 5,874 Apr-13-2017, 03:14 AM
Last Post: cyberzen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020