Hey, Minh Vu again with 2+ years working with Python here.
In this tutorial, I will show you how to capitalize the first letter of each word in a string in Python.
In short, there are 4 ways to capitalize the first letter of a string in Python:
- Using the capitalize() method
- Using the capwords() method
- Using the title() method
- Using string slicing
If you only want the FIRST LETTER of the string to be capitalized, see section 1.
If you want EACH WORD to be capitalized or uppercased, see section 2, 3 and 4.
Using the capitalize() Method
Python's built-in capitalize()
method can be used to capitalize the FIRST LETTER of a string.
See this example:
text = "hello, wisecode. this is minh vu" text_capitalized = text.capitalize() print(text_capitalized) # Hello, wisecode. this is minh vu
As you can see, only the first letter is capitalized or uppercased.
Using the capwords() Method
The capwords()
method is similar to the title()
method. It capitalizes the first letter of each word in a string.
import string text = "hello, wisecode. this is minh vu" text_capitalized = string.capwords(text) print(text_capitalized) # Hello, Wisecode. This Is Minh Vu
For this technique, you can also specify the separator character. By default, it's a space character.
I will make an example by using the letter e
to be the separator:
import string text = "hello, wisecode. this is minh vu" text_capitalized = string.capwords(text, sep="e") print(text_capitalized) # HeLlo, wiseCode. this is minh vu
As you can see, only the first letters after the letter e
are capitalized.
I highly recommend using this method as it's very simple and efficient.
Using the title() Method
Different from the previous technique, the title()
method capitalizes the first letter of EACH WORD in a string.
text = "hello, wisecode. this is minh vu" text_capitalized = text.title() print(text_capitalized) # Hello, Wisecode. This Is Minh Vu
Cool, the result is Hello, Wisecode. This Is Minh Vu
, exactly what we want.
But if you want to understand how it works, let's use a more manual approach, using string slicing. Continue reading.
Using String Slicing
The idea is to split the string into words, capitalize the first letter of each word, and then join them back together.
text = "hello, wisecode. this is minh vu" words = text.split() words_capitalized = [word.capitalize() for word in words] text_capitalized = " ".join(words_capitalized) print(text_capitalized) # Hello, Wisecode. This Is Minh Vu
Shorter version:
text = "hello, wisecode. this is minh vu" text_capitalized = " ".join([word.capitalize() for word in text.split()]) print(text_capitalized) # Hello, Wisecode. This Is Minh Vu
This method is the combination of using the capitalize()
method and string slicing to capitalize the first letter of each word in a string.
It is not so effective as we have to iterate through the list of words twice. But it's still good to know how it works.
Conclusion
That's all. Let's summarize the three ways to capitalize the first letter of a string in Python:
- Using the capitalize() method:
text.capitalize()
- Using the capwords() method:
string.capwords(text)
(recommended) - Using the title() method:
text.title()
(recommended) - Using string slicing:
" ".join([word.capitalize() for word in text.split()])
To me, the capwords()
and title()
method are the most appropriate one if you want to uppercase the first letter of each word in a string. And the capitalize()
method is the best if you only want the first letter to be capitalized.