Sometimes, we want to capitalize the first letter of each word in a string with Python.
In this article, we’ll look at how to capitalize the first letter of each word in a string with Python.
How to capitalize the first letter of each word in a string with Python?
To capitalize the first letter of each word in a string with Python, we can use the string’s title
method.
For instance, we write:
s = "hello world".title()
print(s)
We call title
on "hello world"
to return the string with the first letter of each word capitalized.
And then we assign the returned string to s
.
Therefore, s
is 'Hello World'
.
Conclusion
To capitalize the first letter of each word in a string with Python, we can use the string’s title
method.