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()]
Question
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()]
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
# Function to split email IDs into usernames and domain names
def split_emails(email_tuple):
usernames = tuple(email.split('@')[0] for email in email_tuple)
domains = tuple(email.split('@')[1] for email in email_tuple)
return usernames, domains
# Input number of students
n = int(input("Enter number of students: "))
# Initialize an empty tuple to store email IDs
emails = tuple()
# Read email IDs
for i in range(n):
email = input(f"Enter email ID of student {i+1}: ")
emails += (email, )
# Split email IDs into usernames and domains
usernames, domains = split_emails(emails)
# Print all three tuples
print("Emails:", emails)
print("Usernames:", usernames)
print("Domains:", domains)
Explanation:
The function
split_emails
splits each email ID into a username and a domain and stores them in separate tuples.The program inputs the number of students and then reads their email IDs, storing them in a tuple called
emails
.The
split_emails
function is called to split theemails
tuple into usernames and domains.Finally, it prints the original
emails
tuple, theusernames
tuple, and thedomains
tuple.
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month