Answer:
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.