Capitalize First Letter of Each Word in Python

Capitalize the first letter of each word in a sentence.

BeginnerString ProgramsExample 11 of 25
capitalize-first-letter-of-each-word.py
Run in browser
1# Program to capitalize first letter of each word
2
3s = input("Enter a sentence: ")
4
5result = s.title()
6
7print("Title case:", result)

Output

Enter a sentence: hello world
Title case: Hello World

What's going on

We use .title() which capitalizes the first character of each word and lowercases the rest.