Ask Question
10 January, 11:26

Many documents use a specific format for a person's name. Write a program whose input is: firstName middleName lastName, and whose output is: lastName, firstName middleInitial. Ex: If the input is Pat Silly Doe, the output is: Doe, Pat S. If the input has the form firstName lastName, the output is lastName, firstName.

+4
Answers (1)
  1. 10 January, 14:44
    0
    Python Program:

    if __name__ = = '__main__':

    a = input ("Enter Name: ")

    b = a. split ()

    print (b[2] + ',', b[0], b[1][0])

    Explanation:

    The input function will be used to prompt user to enter the name of the person. The person's name will be stored in variable a.

    Let us assume that the user entered a name mentioned in the example, which is Pat Silly Doe, Now variable a = 'Pat Silly Doe'.

    The function a. split () is used to split a string into a list. The splitting (by default) is done on the basis of white-space character. Therefore, a. split () will give a list

    ['Pat', 'Silly', 'Doe']

    which will be later on stored in variable b.

    We can use the subscript operator [] to access the values in a list. Suppose if we have a list a, then a[i] will give us ith element of a, if i < length of a.

    Finally, we print the answer. b[2] will give us the last name of the person. We append a comma using '+' operator. b[0] will give us the first name. b[1] will give us the middle name, but since we only need one character from the middle name, we can use another subscript operator b[1][0] to give us the first character of the middle name.

    Note: Characters of strings can be accessed using subscript operator too.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Many documents use a specific format for a person's name. Write a program whose input is: firstName middleName lastName, and whose output ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers