Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to print the initials of a name with last name in full?
In this article, we are going to learn how to print the initials of a name with last name in full. For example, in applications like resumes or media references, instead of writing "Sai Vamsi Srinivas", we might write "S.V. Srinivas".
This kind of formatting improves readability and ensures the important part of the name (last name) is fully visible.
Using Python split() and join() Methods
The Python split() method splits all words in a string using a specified separator. The separator can be a space, comma, or any other character. Following is the syntax ?
str.split(separator)
join() Method
The Python join() method joins all elements in an iterable (such as list, string) separated by the given separator. Following is the syntax ?
str.join(sequence)
In this approach, we use the split() method to split the full name into words, then take the first character of all names except the last, and combine them with periods using the join() method.
Example
Let's look at the following example where we convert "Sai Vamsi Srinivas" to initials format ?
def format_name_with_initials(full_name):
words = full_name.strip().split()
# Check if name has at least 2 parts
if len(words) < 2:
return "Error: Name must have at least two parts"
# Get initials from all names except the last
initials = [word[0].upper() for word in words[:-1]]
# Get the last name and capitalize it
last_name = words[-1].capitalize()
# Join initials with dots and add last name
return ".".join(initials) + ". " + last_name
# Test the function
print(format_name_with_initials("Sai Vamsi Srinivas"))
print(format_name_with_initials("John Doe"))
print(format_name_with_initials("Mary Elizabeth Johnson"))
The output of the above program is ?
S.V. Srinivas J. Doe M.E. Johnson
Using String Slicing Method
We can also achieve the same result using string slicing to extract the first character from each name part ?
def format_name_slicing(full_name):
name_parts = full_name.strip().split()
if len(name_parts) < 2:
return "Error: Invalid name format"
# Create initials string
initials = ""
for i in range(len(name_parts) - 1):
initials += name_parts[i][0].upper() + "."
# Add last name
initials += " " + name_parts[-1].capitalize()
return initials
# Test the function
print(format_name_slicing("Albert Einstein"))
print(format_name_slicing("Marie Curie"))
The output is ?
A. Einstein M. Curie
Comparison
| Method | Approach | Best For |
|---|---|---|
| split() + join() | List comprehension | Cleaner, more Pythonic |
| String slicing | Loop-based concatenation | Step-by-step understanding |
Conclusion
Both methods effectively format names with initials and full last name. The split() and join() approach is more concise and Pythonic, while string slicing provides better understanding of the step-by-step process.
