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:
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 –
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
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 –