Q.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()]
Build the email tuple by concatenating one-element tuples in a loop, then use split("@") to break each ID into its username and domain and collect those into two more tuples.
The idea. Tuples are immutable — you cannot append to one. The standard workaround is concatenation: emails = emails + (email,) builds a new, longer tuple each time (note the one-element tuple (email,) with its comma). The parsing itself is one call: every email ID has the shape username@domain, so email.split("@") gives a two-element list we can unpack directly into (username, domain).
Program:
n = int(input("How many email IDs? "))
emails = ()
for i in range(n):
email = input("Enter email ID: ")
emails = emails + (email,)
usernames = ()
domains = ()
for email in emails:
(username, domain) = email.split("@")
usernames = usernames + (username,)
domains = domains + (domain,)
print("Email IDs :", emails)
print("Usernames :", usernames)
print("Domains :", domains)
Sample run:
How many email IDs? 3
Enter email ID: pooja@gmail.com
Enter email ID: arif@yahoo.com
Enter email ID: mary@outlook.com
Email IDs : ('pooja@gmail.com', 'arif@yahoo.com', 'mary@outlook.com')
Usernames : ('pooja', 'arif', 'mary')
Domains : ('gmail.com', 'yahoo.com', 'outlook.com')
Key lines:
emails + (email,)— tuple concatenation;(email,)must carry the trailing comma or it is just a string in brackets.(username, domain) = email.split("@")—splitreturns['pooja', 'gmail.com']for the first ID, and tuple-style unpacking assigns both parts in one statement.
If an address might contain more than one @ (rare but possible in test data), email.split("@", 1) splits only at the first one, keeping the rest intact in the domain part.
The program stores all email IDs in one tuple, then uses split("@") to derive a usernames tuple ('pooja', 'arif', 'mary') and a domains tuple ('gmail.com', 'yahoo.com', 'outlook.com'), printing all three.
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.