Write a program to read email IDs of n number of students and store them in a tuple. Create two new tuples, one to store only the usernames from the email IDs and second to store domain names from the email ids. Print all three tuples at the end of the program. [Hint: You may use the function split()]
Answers
Explanation:
n
= int(input("Enter number of students : "))
list1=[]
for i in range(n):
email=input("Enter email: ")
list1.append(email)
tuple1=tuple(list1)
names=[]
domains=[]
for i in tuple1:
name,domain = i.split("@") #return list of strings break using the string in the argument
names.append(name) #build names list
domains.append(domain) #build domains list
names = tuple(names)
domains = tuple(domains)
print("Names = ",names)
print("Domains = ",domains)
print("Tuple = ",tuple1)
Answer:
from typing import Tuple
def extract_username_domain(email: str) -> Tuple[str, str]:
username, domain = email.split('@')
return username, domain
n = int(input("Enter the number of students: "))
# Create a tuple to store email IDs
email_ids = tuple(input("Enter email ID of student " + str(i+1) + ": ") for i in range(n))
# Create two new tuples to store username and domain
usernames, domains = zip(*(extract_username_domain(email) for email in email_ids))
# Print all three tuples
print("Email IDs:", email_ids)
print("Usernames:", usernames)
print("Domains:", domains)
Explanation:
This program prompts the user to enter the number of students, then prompts the user to enter the email IDs of each student. It stores the email IDs in a tuple. Then, it creates two new tuples: one to store the usernames from the email IDs and the second to store the domain names from the email IDs. It uses the zip() function and the extract_username_domain() function to extract the username and domain from the email. The extract_username_domain() function uses the split() method to separate the email string into username and domain. Finally, the program prints all three tuples at the end of the program.
More question and answers:
https://brainly.in/question/13276803
https://brainly.in/question/29468164
#SPJ3