capitalize first letter of each word in string

Python – Capitalize First Letter of Each Word

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.

capitalize first letter of each word in string

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.

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 –

  1. 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.
  2. For each word, capitalize the first letter using the string upper() function.
  3. 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:

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.

You can also use the string title() function. Let’s look at an example –

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# 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.

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 –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top