235k views
2 votes
write a program that prompts the user to enter a list of names. each person's name is separated from the next by a semi-colon and a space ('; ') and the names are entered lastname, firstname (i.e. separated by ', '). your program should then print out the names, one per line, with the first initial of the first name, followed by ".", and followed by the last name.

User Tijmen
by
6.0k points

2 Answers

5 votes

Final answer:

A Python program is provided to take a list of names input by the user, formatted as 'lastname, firstname', and outputs them as 'F. Lastname', where 'F' is the first initial of the given name.

Step-by-step explanation:

The student has requested a program in which the user enters a list of names separated by semicolons and formatted as lastname, firstname. The program should then display these names with just the first initial of the first name, followed by a period, and then the last name. Below is an example program written in Python to achieve this:

def format_names(name_list):
formatted_names = []
for name in name_list:
last, first = name.split(", ")
formatted_names.append(first[0] + ". " + last)
return formatted_names

# Prompt the user for a list of names
user_input = input("Enter a list of names separated by '; ': ")

# Split the input based on the delimiter '; '
name_list = user_input.split("; ")

# Call the function and capture the result
formatted_names = format_names(name_list)

# Print the formatted names
for name in formatted_names:
print(name)

This script will take user input, parse the names, and then reformat and print them as requested.

User Mike Stay
by
6.3k points
5 votes

Answer:

names = input()

names = names.split(';')

for name in names:

splited_name = name.split(',')

print( splited_name[1][0] , '.' , splited_name[0])

Step-by-step explanation:

I'm gonna give you a answer in python , and explane each line next

Step 1: Read the string of names

names = input()

Step 2: Get splited each name by semicolon ';'

names = names.split(';')

Step 3: Loop in all names

for name in names:

Step 4: Get a name and last name splited:

splited_name = name.split(',')

Step 5: Print the first character of the name with '.' and the last name

print( splited_name[1][0] , '.' , splited_name[0])

User Aiman Kh
by
5.2k points