SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
More About Strings
Exercise:
Programming Exercises
Question:1 | ISBN:9780132576376 | Edition: 2

Question

Write a program that gets a string containing a person’s first, middle, and last names, and then display their first, middle, and last initials. For example, if the user enters John William Smith the program should display J. W. S. 

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#Gets a string containing a person’s first, middle, and last names,
#and then display their first, middle, and last initials. 
def displayInitials(fullName):
    names = fullName.split()
    initials = [name[0].upper() + '.' for name in names]
    print(' '.join(initials))


# Get the full name from the user
fullName = input("Enter your full name: ")

# Display the initials
displayInitials(fullName)

Executed Output 1:

Enter your full name: John William Smith
J. W. S.

Executed Output 2:

Enter your full name: William Stallings Tony
W. S. T.

 

0 0

Discussions

Post the discussion to improve the above solution.