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()]
To solve this problem, we first accept the number of students 'n' from the user. We then initialize an empty tuple to store the email IDs. Using a for loop, we input each email ID and concatenate it to the tuple. Since tuples are immutable, we create a new tuple in each iteration to store the data.
Next, we initialize two empty tuples: one for usernames and another for domain names. We iterate through the main 'emails' tuple. Inside the loop, we use the split() function with the separator '@' on each email ID. This splits the string into two parts: the part before '@' becomes the username, and the part after '@' becomes the domain name.
We extract these split parts and add them to their respective tuples using concatenation. Finally, we print all three tuples as required.
python
Program to store emails and separate usernames and domains
n = int(input("Enter the number of students: ")) emails = ()
for i in range(n): eid = input("Enter email ID: ") emails = emails + (eid,)
usernames = () domains = ()
for email in emails: parts = email.split('@') usernames = usernames + (parts[0],) domains = domains + (parts[1],)
print("Email IDs:", emails) print("Usernames:", usernames) print("Domain Names:", domains)
Explanation
The question requires creating a program involving tuple creation and string manipulation. The solution uses the hint provided in the context to split the email string. It follows the pattern seen in the context (like Program 10-1) of iterating through a tuple. The logic involves creating an empty tuple and appending data via concatenation because tuples are immutable, which aligns with the concept that variables are 'rebuilt' for immutable types as mentioned in the context.
Solution Steps
Step 1: Input the count of students 'n' and initialize an empty tuple 'emails'.
Step 2: Use a loop to input email IDs 'n' times and store them in the 'emails' tuple using concatenation.
Step 3: Initialize two empty tuples 'usernames' and 'domains'.
Step 4: Loop through the 'emails' tuple, use split('@') to separate the username and domain, and store them in their respective tuples.
Step 5: Print the 'emails', 'usernames', and 'domains' tuples.