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.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Enum Class:

Here is another classy approach from Python, no pun intended. Python's enum class will let you create enum object members with constant and unique values which then can be listed, compared or used for identification.

Enum classes are also iterable so they can be iterated as well:

from enum import Enum
class sports(Enum):
    volleyball = 1
    soccer = 2
    running = 3
    football = 4
    baseball = 5
    boxing = 6
print(sports.baseball)
print(repr(sports.baseball))

Output:

sports.baseball
<sports.baseball: 5>