
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to print the initials of a name with last name in full?
In this article, we are going to learn about how to print the initials of a name with last name in full. For example, If we consider the applications like resumes or media references, it represents the person name using initials followed by the last name like instead of writing "Sai Vamsi Srinivas", we might write "S.V.Srinivas".
This kind of formatting improves the readability and ensures the important part of the name(last name) is fully visible.
Using Python split() and join() Methods
The Python split() method is used to split all the words in the string by using the specified separator. The separator can be comma, full-stop or any other character ti separate strings. Following is the syntax of Python's split() method -
str.split(separator)
join() Method
The Python join() method is used to join all the elements in an iterable(such as list, string) separated by the given separator. Following is the syntax of Python split() method -
str.join(sequence)
In this approach, we are going use the split() method for splitting full name into the words, then we will consider the first character of the first two names and they are combined with periods in between using the join() method.
Example
Let's look at the following example, where we are going to consider the string as "Sai Vamsi Srinivas" and observing the output.
def demo(a): x = a.strip().split() if len(x) < 2: return "error" y = [word[0].upper() for word in x[:-1]] z = x[-1].capitalize() return ".".join(y) + ". " + z print(demo("Sai Vamsi Srinivas"))
The output of the above program is as follows -
S.V. Srinivas