Open In App

Python | Print the initials of a name with last name in full

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given a name, print the initials of a name(uppercase) with last name(with first alphabet in uppercase) written in full separated by dots. 

Examples:

Input : geeks for geeks
Output : G.F.Geeks

Input : mohandas karamchand gandhi
Output : M.K.Gandhi 

A naive approach of this will be to iterate for spaces and print the next letter after every space except the last space. At last space we have to take all the characters after the last space in a simple approach. Using Python in inbuilt functions we can split the words into a list, then traverse till the second last word and print the first character in capitals using upper() function in python and then add the last word using title() function in Python which automatically converts the first alphabet to capital. 

Implementation:

Python3




# python program to print initials of a name
def name(s):
 
    # split the string into a list
    l = s.split()
    new = ""
 
    # traverse in the list
    for i in range(len(l)-1):
        s = l[i]
         
        # adds the capital first character
        new += (s[0].upper()+'.')
         
    # l[-1] gives last item of list l. We
    # use title to print first character in
    # capital.
    new += l[-1].title()
     
    return new
     
# Driver code           
s ="mohandas karamchand gandhi"
print(name(s))       


Output

M.K.Gandhi

The time complexity of this algorithm is O(n) where n is the number of words in the string. This is because the algorithm only iterates through the string once.

The space complexity of this algorithm is O(n) because a new string is created and all the words in the string are stored in a list.

Using regular expressions:

Algorithm:

  1. Import the re module for using regular expressions.
  2. Define a function name which takes a string as input.
  3. Use the re.findall() function to extract the first letter of each word in the string s. The regular expression ‘\b\w’ matches the first letter of each word. The ‘\b’ specifies a word boundary and ‘\w’ matches any alphanumeric character.
  4. Use the re.findall() function again to extract the last word in the string s. The regular expression ‘\b\w+$’ matches the last word in the string. The ‘$’ specifies the end of the string.
  5. Combine the initials and last name with dots using the join() function. The join() function concatenates the elements of a list into a string using a specified delimiter.
  6. Convert the initials to uppercase and last name to title case (i.e. capitalize the first letter of each word).
  7. Return the result.

Python3




import re
 
def name(s):
  # Use regular expression to extract the first letter of each word
  initials = re.findall(r'\b\w', s)
   
  # Use regular expression to extract the last word
  last_name = s.split()[-1]
  # Combine the initials and last name with dots
  result = '.'.join(initials[:-1]).upper() + '.' + last_name.title()
 
  return result
 
#Driver code
s = "mohandas karamchand gandhi"
print(name(s))


Output

M.K.Gandhi

Time complexity: The time complexity of the regular expression approach is O(n), where n is the length of the input string s. This is because the re.findall() function iterates over the string s once to extract the required characters.

Space complexity: The space complexity of the regular expression approach is O(n), where n is the length of the input string s. This is because the re.findall() function creates a list to store the extracted characters, and the result string also takes up space proportional to the length of s.



Last Updated : 23 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads