One of the essential features of any programming language is the ability to manipulate strings. In this article, we’ll explore a simple yet powerful Python string method called title()
. We will see how to use this method to capitalize the first letter of each word in a given string.
The title()
method is a built-in Python string method that capitalizes the first letter of each word in a given string. It returns a new string where the first letter of each word is capitalized, and all other letters are in lowercase. Here is the syntax of the title()
method:
string.title()
As we can see, the title()
method is called on a string object and returns a new string with the first letter of each word capitalized.
Example Usage of title()
Let’s consider an example to understand the usage of the title()
method. Suppose we have a string as follows:
s = "programming is awesome"
If we want to capitalize the first letter of each word in the above string, we can simply call the title()
method on the string s
:
print(s.title())
The output of the above code would be:
Programming Is Awesome
As we can see, the title()
method has capitalized the first letter of each word in the string and returned a new string where each word starts with a capital letter.
That’s it.
I hope you find this helpful.