Python Exercises: Extract the name from an Email address
From Wikipedia –
An email address consists of two parts, a local part[a] and a domain; if the domain is a domain name rather than an IP address then the SMTP client uses the domain name to look up the mail exchange IP address. The general format of an email address is local-part@domain, e.g. jsmith@[192.168.1.2], [email protected].
Write a Python program to extract and display the name from a given email address.
Sample Data:
(“[email protected]”) -> (“john”)
(“[email protected]”) -> (“johnsmith”)
(“[email protected]”) -> (“fullyqualifieddomain”)
Sample Solution-1:
Python Code:
def test(email_address):
r = email_address.index("@")
return "".join(l for l in email_address[:r] if l.isalpha())
email_address = "[email protected]"
print("Original Email:", email_address)
print("Extract the name from the said Email address:")
print(test(email_address))
email_address = "[email protected]"
print("\nOriginal Email:", email_address)
print("Extract the name from the said Email address:")
print(test(email_address))
email_address = "[email protected]@example.com"
print("\nOriginal Email:", email_address)
print("Extract the name from the said Email address:")
print(test(email_address))
email_address = "[email protected]"
print("\nOriginal Email:", email_address)
print("Extract the name from the said Email address:")
print(test(email_address))
Sample Output:
Original Email: [email protected] Extract the name from the said Email address: john Original Email: [email protected] Extract the name from the said Email address: johnsmith Original Email: [email protected]@example.com Extract the name from the said Email address: disposablestyleemailwithsymbol Original Email: [email protected] Extract the name from the said Email address: fullyqualifieddomain
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Sample Solution-2:
Python Code:
def test(email_address):
data = email_address.split("@")[0]
result = ""
for i in data:
if i.isalpha():
result+=i
return result
email_address = "[email protected]"
print("Original Email:", email_address)
print("Extract the name from the said Email address:")
print(test(email_address))
email_address = "[email protected]"
print("\nOriginal Email:", email_address)
print("Extract the name from the said Email address:")
print(test(email_address))
email_address = "[email protected]@example.com"
print("\nOriginal Email:", email_address)
print("Extract the name from the said Email address:")
print(test(email_address))
email_address = "[email protected]"
print("\nOriginal Email:", email_address)
print("Extract the name from the said Email address:")
print(test(email_address))
Sample Output:
Original Email: [email protected] Extract the name from the said Email address: john Original Email: [email protected] Extract the name from the said Email address: johnsmith Original Email: [email protected]@example.com Extract the name from the said Email address: disposablestyleemailwithsymbol Original Email: [email protected] Extract the name from the said Email address: fullyqualifieddomain
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous Python Exercise: Sort a string based on its first character.
Next Python Exercise: Python List Exercise Home.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.