Thursday, February 7, 2019

codingdirectional: 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.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...