Python program to capitalize each word's first letter

Suppose we have a sentence with English lowercase letters. We have to convert first letter of each word into uppercase.

So, if the input is like s = "i love my country", then the output will be "I Love My Country"

To solve this, we will follow these steps −

  • words := list of words from s
  • ret := a new blank list
  • for each i in words, do
    • capitalize first letter of i using capitalize() function and insert it into ret
  • join each words present in ret separated by blank space and return


Let us see the following implementation to get better understanding

def solve(s):
   words = s.split(' ')
   ret = []
   for i in words:
   return ' '.join(ret)

s = "i love my country"


"i love my country"


I Love My Country