Python | Print the initials of a name with last name in full
Last Updated :
23 Mar, 2023
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
def name(s):
l = s.split()
new = ""
for i in range ( len (l) - 1 ):
s = l[i]
new + = (s[ 0 ].upper() + '.' )
new + = l[ - 1 ].title()
return new
s = "mohandas karamchand gandhi"
print (name(s))
|
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:
- Import the re module for using regular expressions.
- Define a function name which takes a string as input.
- 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.
- 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.
- 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.
- Convert the initials to uppercase and last name to title case (i.e. capitalize the first letter of each word).
- Return the result.
Python3
import re
def name(s):
initials = re.findall(r '\b\w' , s)
last_name = s.split()[ - 1 ]
result = '.' .join(initials[: - 1 ]).upper() + '.' + last_name.title()
return result
s = "mohandas karamchand gandhi"
print (name(s))
|
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.
Please Login to comment...