65.0k views
5 votes
Write a function get_initials(the name) that takes a string of a name and returns the initials of the name, in upper case, separated by periods, with an additional period at the end. Input-Output Samples John Q Public J.Q.P. Homer J Simpson H.J.S. Gloria kind of a big deal Tropalogos G.O.O.D. If the argument to the function is the string on the left, then return the string on the right. You don't need to print, since that's the job of wherever the value is returned.

1 Answer

5 votes

Answer:

Following are the code to this question:

def get_initials(the_name): #defining a method get_initials

name = the_name.split(" ") #defining name variable that splits value

val = ''#defining a string variable val

for n in name: #defining loop to convert value into upper case and store first character

val = val + n[0].upper() + "." # use val variable to hold value

return val # return val

print(get_initials("Gloria kind of a big deal Tropalogos"))#call method and print return value

print(get_initials("Homer J Simpson"))#call method and print return value

print(get_initials("John Q Public")) #call method and print return value

Output:

J.Q.P.

H.J.S.

G.K.O.A.B.D.T.

Explanation:

In the given code, the last is incorrect because if we pass the value that is "Gloria kind of a big deal Tropalogos" so, will not give G.O.O.D. instead of that it will return G.K.O.A.B.D.T. So, the program description can be given as follows:

  • In the given python code, a method "get_initials" is declared, that accepts "the_name" variable as a parameter, inside the method "name" variable is declared, which uses the split method that splits all values.
  • In the next step, a string variable "val" and for loop is used, in which the "val" variable converts the first character of the string into the upper case and stores its character with "." symbol in "val" variable and return its value.
  • In the last step, the print method is used, that passes the string value and prints its sort value.
User Md Monjur Ul Hasan
by
4.9k points