Python | Type the initials of the first name with the last name in full

Python Methods and Functions

Examples :

 Input: geeks for geeks Output: GFGeeks Input: mohandas karamchand gandhi Output: MKGandhi 

Naive the approach to this would be to repeat spaces and print the next letter after every space except for the last space. At the end of the space, we have to take all the characters after the last space in a simple way.

Using Python in built-in functions we can split words into a list, then go to the second last word and print the first character in capital letters using the function isupper-islower-lower-upper-python-applications/>upper () in python and then add the last word using isupper-lower-upper-swapcase-title /> title () in Python that automatically converts the first alphabet to uppercase

# python program to print name initials

def name (s):

 

# split the string into a list

l = s.split ()

new = ""

 

# move in the list

  for i in range ( len (l) - 1 ):

s = l [i]

  

# adds capital first letter

  new + = (s [ 0 ]. upper () + '.' )

  

# l [-1] gives the last item in the l list. We

# use title to print the first character in

# capital.

new + = l [ - 1 ]. title ()

 

return new 

 
# Driver code

s = "mohandas karamchand gandhi"  

print (name (s)) 

Output:

 MKGandhi 




Tutorials