Python program to capitalize each word's first letter

PythonServer Side ProgrammingProgramming

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
raja
Updated on 11-Oct-2021 12:34:20

Advertisements