Dremendo Tag Line

Input a sentence and change the first alphabet to capital and rest to small letters in Python

String Methods - Question 10

In this question, we will see how to input a sentence and change the first alphabet of every word to capital and rest to small letters in Python programming. To know more about string methods click on the string methods lesson.

Q10) Write a program in Python to input a sentence and change the first alphabet of every word to capital and rest to small letters. Assume that input may be in any case.

Program

ns=""
i=0
str=input('Enter a sentence\n')

str=str.lower()
while i<len(str):
    if i==0 and str[i]!=' ':
        ns=ns+str[i].upper()

    elif str[i]==' ' and str[i+1]!=' ':
        ns=ns+' '+str[i+1].upper()
        i=i+1

    else:
        ns=ns+str[i]

    i=i+1

print(ns)

Output

Enter a sentence
a quiCk bRown fox juMP oVeR the LAZy DOG
A Quick Brown Fox Jump Over The Lazy Dog
video-poster