In: Computer Science
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, firstInitial.middleInitial.
If the input has the form:
firstName lastName
the output is:
lastName, firstInitial.
Ex: If the input is:
user_input = input()
splits = user_input.split(" ")
if(len(splits) == 3):
first_name = splits[0]
middle_name = splits[1]
last_name = splits[2]
print(last_name + ", " + first_name[0] + "." + middle_name[0] +
".")
elif(len(splits) == 2):
first_name = splits[0]
middle_name = splits[1]
print(middle_name + ", " + first_name[0] + ".")
I got helped on this, but someone explain to me line by line what it means? Splits == 3, how do I know it's three?! the [0] and [1] why does first name have[0] what does the 0, 1 mean?!!
Program: In this program, we are taking input from the user, and then after the processing the string we output the string in specific format.
Steps:
We can access any character of a string using its indices, for e.g - fruit = "apple", if we print fruit[0] we get 'a' as output, if we print fruit[1] we get 'p' as output and similarly if we print fruit[4] we get 'e' as output.
Below is the code with comments for more visualisation:
Code:
user_input = input()
# The slit() function splits the string input based on the regex passed in it
# and returns a list of strings, here we are storing it in a variable named splits
# Here we passed space as our regex and hence our input would be split based on the spaces in the string input
# for e.g - input => Mary Elizabeth Smith
# splits = ["Mary", "Elizabeth", "Smith"]
splits = user_input.split(" ")
# If user input was - firstName middleName lastName
# splits would have a list of size 3 like this ["firstName","middleName","LastName"]
# at index 0 we have first name, at index 1 middle name, and at index 2 last name
if(len(splits) == 3):
# Store firstName, first element of splits list
first_name = splits[0]
# Store middleName, second element of splits list
middle_name = splits[1]
# Store lastName, third element of splits list
last_name = splits[2]
# As we want to access the first characters of first name and middle name
# the first character is stored at 0th index, first_name[0] would give first character of first name
# and middle_name[0] would give first character of middle name
# Print lastName, firstInitial.middleInitial.
print(last_name + ", " + first_name[0] + "." + middle_name[0] + ".")
# else if the input was - firstName lastName
# splits would have a list of size 2 like this ["firstName","lastName"]
# at index 0 we have first name, at index 1 last name
elif(len(splits) == 2):
#Store firstName, first element of splits list
first_name = splits[0]
#Store lastName, second element of splits list
middle_name = splits[1]
# As we want to access the first character of first name and middle name
# the first character is stored at 0th index, first_name[0] would give first character of first name
#Print lastName, firstInitial.
print(middle_name + ", " + first_name[0] + ".")
Code image:
Output 1:
Output 2:
# Please ask for any doubts. Thanks.