Home / Python / Example programs / Strings

Python program: Capitalise the first letter of each word

Turn "hello world" into "Hello World".

The code

text = "the quick brown fox"
print(text.title())
print(" ".join(word.capitalize() for word in text.split()))
print(text[0].upper() + text[1:])     # only the very first letter

Output

The Quick Brown Fox
The Quick Brown Fox
The quick brown fox

How it works

title() does it in one call. The split/capitalize/join version gives you control, for example to leave small words like "of" alone.

Python features used

Learn the ideas behind it in the free lesson Strings.