Write a program that takes a string with multiple words and capitalizes the first letter of each word and forms a new string out of it.

 

Answer:

Untitled 34.jpg

Code:

str=input( "Enter a string: " )

l=len(str)

i= 0

str2= ""

while i<l:

    if i == 0 :

        str2 = str2+str[i].upper()

    elif str[i]== ' ' and str[i+ 1 ]!= ' ' :

        str2=str2+str[i]

        str2=str2+str[i+ 1 ].upper()

        i=i+ 1

    else:

        str2 = str2+str[i]

    i=i+ 1

print( "New string: " +str2) 

 

Explanation:

The first letter of the string and every letter after a blank space is converted to uppercase using the upper( ) method.

Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.