w3resource

Python Exercises: Extract the name from an Email address

Python String: Exercise-105 with Solution

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 [email protected], e.g. [email protected][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:

Flowchart: Extract the name from an Email address.

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:

Flowchart: Extract the name from an Email address.

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: Capitalize the first letter and lowercases the rest.
Next Python Exercise: Replace repeated characters with single letters.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Getting sorted iterable (can sort by "compare" function):

>>> a = [1, 2, -3]
>>> sorted(a)
[-3, 1, 2]

>>> sorted(a,key=abs)
[1, 2, -3]