LearnFrenzy
Sign in Take a free test

Python Basics and Syntax · Python Basics Coding Problems · Question 10

Print initials and a username from a full name in Python Solution, output & explanation

  • Program
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

Read a full name such as sachin ramesh tendulkar and print the initials in capitals with dots, and a username made of the first name and the length of the full name.

Solution
main.py Run
full = input().strip()
parts = full.split()                 # ['sachin', 'ramesh', 'tendulkar']

initials = ""
for p in parts:
    initials += p[0].upper() + "."

username = parts[0].lower() + str(len(full.replace(" ", "")))

print("Name:    ", full.title())
print("Initials:", initials)
print("Username:", username)

Output Verified by running it on Python 3

Name:     Sachin Ramesh Tendulkar
Initials: S.R.T.
Username: sachin21

split() breaks the name into words, p[0] is each first letter, and title() capitalises every word. The username counts letters only (spaces removed): 6 + 6 + 9 = 21.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links