Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to display initials in Python

I'm trying to make this so that when a person types their name just the initials display capitalized and separated by a period. I can't figure out what is wrong with this code I wrote... help pls!

def main():

name = input('Type your name and press ENTER. ')
name_list = name.split()

print(name_list)

first = name[0][0]
second = name[1][0]
last = name[2][0]

print(first,'.', second,'.')

main()
like image 393
user3534918 Avatar asked Feb 14 '23 04:02

user3534918


1 Answers

If you are on Python 2.x you should exchange input for raw_input. Here's a quicker way to achieve what you're aiming for assuming you're on Python 2.x:

def main():
    full_name = raw_input('Type your name and press ENTER. ')
    initials = '.'.join(name[0].upper() for name in full_name.split())
    print(initials)
like image 198
agrinh Avatar answered Feb 15 '23 16:02

agrinh