Questions: 2.12 LAB: Name format Many documents use a specific format for a person's name. Write a program that reads a person's name in the following format: firstName middleName lastName (in one line) and outputs the person's name in the following format: lastName, firstInitial.middleInitial. Ex: If the input is: Pat Silly Doe the output is: Doe, P.S. If the input has the following format: firstName lastName (in one line) the output is: lastName, firstInitial. Ex: If the input is: Julia Clark the output is: Clark, J.

2.12 LAB: Name format

Many documents use a specific format for a person's name. Write a program that reads a person's name in the following format: firstName middleName lastName (in one line) and outputs the person's name in the following format: lastName, firstInitial.middleInitial.

Ex: If the input is:
Pat Silly Doe
the output is:

Doe, P.S.
If the input has the following format:
firstName lastName (in one line)
the output is:
lastName, firstInitial.
Ex: If the input is:

Julia Clark
the output is:

Clark, J.
Transcript text: 2.12 LAB: Name format Many documents use a specific format for a person's name. Write a program that reads a person's name in the following format: firstName middleName lastName (in one line) and outputs the person's name in the following format: lastName, firstInitial.middleInitial. Ex: If the input is: Pat Silly Doe the output is: Doe, P.S. If the input has the following format: firstName lastName (in one line) the output is: lastName, firstInitial. Ex: If the input is: Julia Clark the output is: Clark, J.
failed

Solution

failed
failed

To solve this problem, we need to write a Python program that reads a person's name in a specific format and outputs it in a different format. Let's break down the requirements and implement the solution step-by-step.

Requirements:
  1. Input Format:
    • The input can be either "firstName middleName lastName" or "firstName lastName".
  2. Output Format:
    • If the input is "firstName middleName lastName", the output should be "lastName, firstInitial.middleInitial."
    • If the input is "firstName lastName", the output should be "lastName, firstInitial."
Steps to Implement:
  1. Read the input string.
  2. Split the input string into parts.
  3. Determine the format based on the number of parts.
  4. Construct the output string accordingly.
  5. Print the output string.
Implementation:

Here is the Python code to achieve the desired functionality:

# Read the input string
input_name = input().strip()

# Split the input string into parts
name_parts = input_name.split()

# Determine the format based on the number of parts
if len(name_parts) == 3:
    first_name, middle_name, last_name = name_parts
    output = f"{last_name}, {first_name[0]}.{middle_name[0]}."
elif len(name_parts) == 2:
    first_name, last_name = name_parts
    output = f"{last_name}, {first_name[0]}."
else:
    output = "Invalid input format"

# Print the output string
print(output)
Explanation:
  1. Reading Input:
    • input().strip() reads the input and removes any leading or trailing whitespace.
  2. Splitting Input:
    • input_name.split() splits the input string into a list of words.
  3. Determining Format:
    • If the list has three parts, it means the input is in the "firstName middleName lastName" format.
    • If the list has two parts, it means the input is in the "firstName lastName" format.
  4. Constructing Output:
    • For three parts: f"{last_name}, {first_name[0]}.{middle_name[0]}." constructs the output using the first initials of the first and middle names.
    • For two parts: f"{last_name}, {first_name[0]}." constructs the output using the first initial of the first name.
  5. Printing Output:
    • The constructed output string is printed.
Example Runs:
  1. Input:

    Pat Silly Doe
    

    Output:

    Doe, P.S.
    
  2. Input:

    Julia Clark
    

    Output:

    Clark, J.
    

This code handles both specified input formats and produces the correct output as required.

Was this solution helpful?
failed
Unhelpful
failed
Helpful