Aug-23-2019, 01:05 AM
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:
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:
Edit 2: Here is my script now:
#!/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.