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


Messages In This Thread
Slicing and printing two halfs of an email address (Udemy) - by Drone4four - Aug-23-2019, 01:05 AM

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