PythonMastery
Python programs · Strings · beginner

Capitalize the first letter of each word in Python

Make the first letter of each word a capital and keep the rest of the word as it was, so NASA stays NASA. Compared with capitalize() and title().

Split the text into words, change the first letter of each word to a capital, and join the words again. The rest of each word is not changed.

The program

python
text = input("Enter a sentence: ")

new_words = []
for word in text.split():
    new_words.append(word[0].upper() + word[1:])

print(" ".join(new_words))

Typed in this example: the NASA team met in london. Press Run to type your own.

output
Enter a sentence: the NASA team met in london
The NASA Team Met In London

How it works

Compare with capitalize() and title()

Python has two methods that look similar, but both make every other letter small:

python
text = "the NASA team met in london"

print(" ".join(word.capitalize() for word in text.split()))
print(text.title())
output
The Nasa Team Met In London
The Nasa Team Met In London

"NASA" becomes "Nasa" in both. Use capitalize() or title() when the input is all small letters, or when you want the rest of each word in small letters. Use the program above when some words, such as names and short forms, already have capitals that must stay. The title case page shows one more difference between title() and string.capwords().

Learn the idea: Working with Strings, Lists & Sequences, Loops: for, while, and the Pythonic Patterns