Return the first capital character of a name

I am still preparing the material for the next python project and in order to keep this website alive, I will continue to solve easy questions from codewars so at least we have something to talk about while waiting for the next project.

In this next example we will create a python method which will receive a name of a person, then it will only take the first letter from the first name, last name or even the middle name and turns that letter into the capital letter if that character is indeed a lower case letter. All the characters will be concatenated with a ‘.’ before returning back to the caller.

def abbrevName(name):
    name_list = name.split(' ')
    s = ''
    for name in name_list:
        s+=name[0:1].upper() + '.'
        
    return s[0:len(s)-1]

So if we enter below name into that method.

print(abbrevName("Jason stock Bond"))

Then this program will return “J.S.B”

If you have a better solution than mine then let keep it up by leaving your solution in the comment box below this post.

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
8 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
MirkoKurba Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Kurba
Guest
Kurba

Not necessarily easier to understand but I would have done it as follows:

def abbrevName(name):
return ‘.’.join(u[0].upper() for u in name.split(‘ ‘))

Mirko
Guest
Mirko

def abbrevName(name):
return “.”.join(s[0].upper() for s in name.split())