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:

[Class 11] Write a program that takes a string with multiple words - Long Answer Type Questions (5 Marks each)

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.

Remove Ads
Teachoo · Class 11 Explore Class 11
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.