Categories
Python Answers

How to capitalize the first letter of each word in a string with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *