-1

I have solved the problem of how to capitalize the first and forth letter but out of curiosity I am trying to do that for all the words in a list rather than just the first:

def my_function(name):
    if len(name) > 3:
        return name[:3].capitalize() + name[3:].capitalize()
    else:
        return 'Name is too short'

result = my_function('oldmac')
print(result)
sshashank124
  • 31,495
  • 9
  • 67
  • 76
Marius.T
  • 25
  • 7

1 Answers1

0

You could just apply the same function for all the words in your list.

>>> def my_function(name):
...     if len(name) > 3:
...         return name[:3].capitalize() + name[3:].capitalize()
...     else:
...         return 'Name is too short'

>>> list_of_words = ['oldmac', 'newmac', 'latestmac']
>>> capitalized = [my_function(word) for word in list_of_words]
>>> capitalized
['OldMac', 'NewMac', 'LatEstmac']

Something like this.

Anurag A S
  • 725
  • 10
  • 23