Find Initials of a Name in Python

Often in many scenarios Initials of names are used in signatures in various documents and web forms. The initials are a shorter representation of the name of an individual. They are the capitalized form of the starting letter of the first, middle, and last name of the person. For example, in the name ‘Mohandas Karamchand Gandhi’, the initials we can write initials as M.K.G.Other forms include just considering the initials of the first and middle name and keeping the last name as it is. For example, the same name mentioned before is written as M.K. Gandhi. Thus in this tutorial, we will understand how to find the initials of a Name in Python with code examples.

Find initials of Name in Python- Type 1

Initially, we will find the initials of the full name in Python consisting of all the components of the name of the person such as first, middle, and last name. The below-mentioned code considers input from the user for the full name and converts it into its required format.

The split() function of Python splits the full name into its individual component words. Then the for loop iterates over each word and extracts its first character. We convert each initial character of the word to its uppercase using the upper() function. Lastly, we concatenate the letter to a string to form the final initials.

 

def initials(full_name):
  initial=""
  if (len(full_name) == 0):
   return
    
  first_middle_last = full_name.split(" ")
  for name in first_middle_last:
    initial=initial+name[0].upper()+"."
  return initial
                
full_name="Mohandas Karamchand gandhi"
print(f"Initals generated: {initials(full_name)}")

Output:

Initals generated: M.K.G.

Find initials of Name in Python using List comprehension

In the below-mentioned code, a list comprehension contains an expression that is executed for each component along with the for loop to iterate over each component of the full name.

The expression consists of the first character of each component word and is converted into Uppercase. Finally, we concatenate each initial letter together using the join() function. This code follows a much more compact structure than the previously mentioned code.

 

def initials(full_name):
    
    if (len(full_name) == 0):
      return
   
    initial  = ''.join([name[0].upper()+"." for name in full_name.split(' ')])
    return initial
                
full_name="Mohandas Karamchand gandhi"
print(f"Initals generated: {initials(full_name)}")

Output:

Initals generated: M.K.G.

Find initials of Name in Python- Type 2

Now let us just consider the initials of the first and middle name and keep the last name as it is. In the below-mentioned code,  we split the full name into its individual name components into a list. Then we iterate over the list and extract the initial letter of each word contained inside it.

Note that we iterate all the elements of the list except the last element which is the surname component. Finally, we extract the last element (surname) of the list and add it to the initials string. Thus we get the final result.

def initials(full_name):
    if (len(full_name) == 0):
      return
    name = full_name.split()
    initials= ""
   
    for i in range(len(name)-1):
      initial = name[i]
    
      initials += (initial[0].upper()+'.')
  
    initials += name[-1].title()
    return initials
    
full_name="Mohandas Karamchand gandhi"
print(f"Initals generated: {initials(full_name)}")

Output:

Initals generated: M.K.Gandhi

 

Thus we have reached the end of this tutorial. To learn more about list comprehension in Python refer to the following link: List and dictionary comprehension in Python

Leave a Reply

Your email address will not be published. Required fields are marked *