Python - Print the initials of a name with last name in full Last Updated : 15 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report When working with strings, we might need to format a full name such that the initials of the first and middle names are displayed, while the last name is shown in full. Let’s discuss some methods to achieve this in Python.Using split() and a loopsplit() and a loop method splits the name into words and processes the initials for all but the last word, which is then appended as it is. Python # Input name n = "Zoe Kristen Davis" # Split the name into parts parts = n.split() # Process the initials and add the last name res = " ".join([part[0] + "." for part in parts[:-1]]) + " " + parts[-1] print(res) OutputZ. K. Davis Explanation:Splitting the name into a list of words using split().Using a for loop to get the first character of each word, adding a period after it.Joining the initials and append the last name as-is.Let's explore some more methods and see how we can print the initials of a name with last name in full.Table of ContentUsing list comprehensionUsing enumerate in a loopUsing regular expressionsUsing list comprehensionThis method uses a list comprehension for concise processing of initials and directly appends the last name. Python n = "Zoe Kristen Davis" # Split and process the name res = " ".join([word[0] + "." for word in n.split()[:-1]]) + " " + n.split()[-1] print(res) OutputJ. M. Doe Explanation:Here, we split the name using split().Using a list comprehension to process initials for all but the last word.Appending the last word as it is.Using enumerate() in a loopThis method uses enumerate() to process each part of the name, adding initials for all words except the last. Python n = "Zoe Kristen Davis" # Initialize result res = "" # Loop through each part of the name for i, part in enumerate(n.split()): if i == len(n.split()) - 1: res += part # Append the last name else: res += part[0] + ". " # Append initials print(res) OutputZ. K. Davis Explanation:Using enumerate() to loop through the parts of the name.Checking if the current part is the last name; if so, appending it directly.Otherwise, we append the first character with a period.Using regular expressionsThis method uses a regular expression to match each word and extract the initials and last name. Python import re # Input name n = "Zoe Kristen Davis" # Use regex to extract initials and last name parts = re.findall(r'\b\w+', n) res = " ".join([word[0] + "." for word in parts[:-1]]) + " " + parts[-1] print(res) OutputZ. K. Davis Explanation:Using re.findall() to extract words from the string.Processing the initials for all but the last word using a loop or list comprehension.Appending the last word directly. Comment More infoAdvertise with us Next Article Python IMDbPY â Getting Person name from searched name S Striver Follow Improve Article Tags : Python python-list python-string Python list-programs Python string-programs +1 More Practice Tags : pythonpython-list Similar Reads Python IMDbPY â Getting Person name from searched name In this article we will see how we can get the person name from the searched list of name, we use search_name method to find all the related names.search_name method returns list and each element of list work as a dictionary i.e. they can be queried by giving the key of the data, here key will be na 2 min read How To Print A Variable's Name In Python In Python, printing a variable's name can be a useful debugging technique or a way to enhance code readability. While the language itself does not provide a built-in method to directly obtain a variable's name, there are several creative ways to achieve this. In this article, we'll explore five simp 3 min read Python Tweepy - Getting the name of a user In this article we will see how we can get the name of a user. Name is the display name of the twitter account. It is the name that users choose to identify themselves on the network. Many users choose to either use their real name as the basis for their display name. Unlike screen names, the names 2 min read Python | sep parameter in print() The separator between the arguments to print() function in Python is space by default (softspace feature) , which can be modified and can be made to any character, integer or string as per our choice. The 'sep' parameter is used to achieve the same, it is found only in python 3.x or later. It is als 3 min read Comparing and Managing Names Using name-tools module in Python While working with documents, we can have problems in which we need to work with Names. This article discusses the name-tools library which can help to perform this task. It can be used to manage and compare names. It currently includes English and Western Style names. Installation: Use the below co 2 min read Get tag name using Beautifulsoup in Python Prerequisite: Beautifulsoup Installation Name property is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. Name object corresponds to the name of an XML or HTML t 1 min read Like