![]() |
Slicing and printing two halfs of an email address (Udemy) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Slicing and printing two halfs of an email address (Udemy) (/thread-20637.html) |
Slicing and printing two halfs of an email address (Udemy) - Drone4four - Aug-23-2019 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 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. RE: Slicing and printing two halfs of an email address (Udemy) - newbieAuggie2019 - Aug-23-2019 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. RE: Slicing and printing two halfs of an email address (Udemy) - snippsat - Aug-23-2019 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> RE: Slicing and printing two halfs of an email address (Udemy) - ThomasL - Aug-23-2019 Another option would be using string.index() function that you are already aware of in another thread. RE: Slicing and printing two halfs of an email address (Udemy) - perfringo - Aug-23-2019 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 RE: Slicing and printing two halfs of an email address (Udemy) - wavic - Aug-26-2019 (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. |