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

Example

Let us see the following implementation to get better understanding

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

s = "i love my country"
print(solve(s))

Input

"i love my country"

Output

I Love My Country

Updated on: 11-Oct-2021

935 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements