In this tutorial, we will look at how to capitalize the first letter of each word in a Python string with the help of some examples.

There are a number of ways to capitalize the first letter of each word in a string in Python. Let’s look at some with the help of examples.
1. Using string split()
, upper()
, and join()
funtions
You can use a combination of the string split()
, upper()
and join()
functions to capitalize the first letter of each word in a string. Use the following steps –
- Split the string into its constituent words using the string
split()
function. This function splits the string at the occurrence of whitespace characters in the string by default. - For each word, capitalize the first letter using the string
upper()
function. - Join the capitalized words back together using the string
join()
function to get the desired string.
Let’s look at an example –
# create a string s = "the cat and the dog" # capitalize first letter of each word new_s = " ".join(word[0].upper()+word[1:] for word in s.split(" ")) print(new_s)
Output:
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
The Cat And The Dog
You can see that each word in the resulting string starts with a capital letter.
This method is straightforward and does not mess with the capitalization of the other characters in the string.
2. Using string title()
function to capitalize first letter of each word
You can also use the string title()
function. Let’s look at an example –
# create a string s = "the cat and the dog" # capitalize first letter of each word new_s = s.title() print(new_s)
Output:
The Cat And The Dog
The resulting string has the first letter of each word in uppercase.
Note that this function may not give correct results if there are characters such as apostrophes or other uppercase characters present in the string. Here’s an example –
# create a string s = "he's from the USA" # capitalize first letter of each word new_s = s.title() print(new_s)
Output:
He'S From The Usa
You can see that the character just after the apostrophe is also capitalized. Also, note that the case of existing uppercase characters that were not the first character in the word has been altered (“USA” is “Usa”) in the resulting string.
For more on the string title()
function, refer to its documentation.
3. Using string.capwords()
function
The string.capwords()
function can also help you to capitalize the first letter of each word in a string.
import string # create a string s = "the cat and the dog" # capitalize first letter of each word new_s = string.capwords(s) print(new_s)
Output:
The Cat And The Dog
The resulting string has the first letter of each word in uppercase.
Note that this function may not give correct results if there are existing uppercase characters present in the string. Let’s look at an example –
import string # create a string s = "he's from the USA" # capitalize first letter of each word new_s = string.capwords(s) print(new_s)
Output:
He's From The Usa
Here, we were able to mitigate the capitalization of the character just after the apostrophe but the case of some uppercase characters was again altered (“USA” is “Usa” in the resulting string).
For more on the string.capwords() function, refer to its documentation.
If we use the first method, using string split()
, upper()
, and join()
functions we get the correct results.
# create a string s = "he's from the USA" # capitalize first letter of each word new_s = " ".join(word[0].upper()+word[1:] for word in s.split(" ")) print(new_s)
Output:
He's From The USA
The first method is not only intuitive and straightforward but also gives the expected result when it comes to capitalizing the first letter of each word in a string.
You might also be interested in –