Issue
An example would be of the output I am looking for is:
Enter list of names:
Tony Stark, Steve Rodgers, Wade Wilson
Output
T. Stark
S. Rodgers
W. Wilson
I would also appreciate if you could explain how the code for this works because I am new to coding and want to understand what I'm doing rather than just copying.
Solution
First of all you need to read the names from the user:
names_str = input("please enter names separated by commas: ")
Now, the first step to separate them to have a list of "FirstName LastName"s. You'd better first replace all ", " with "," to avoid unnecessary spaces.
first_lasts = names_str.replace(", ", ",").split(",")
Next, you want to print the First Initial. + Last name.
for first_last in first_lasts:
first_name, last_name = first_last.split(" ")
print(f"{first_name[0]}. {last_name}")
There are many more advanced and shorter ways to do it but since you mentioned you're still learning I think this one is a good starting point :)
result:
T. Stark
S. Rodgers
W. Wilson
Answered By - Mohammad
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.