Capitalizing First Letter of Each Word in Python

Introduction

Working with strings is a common task in many programming languages. One possible use-case you'll encounter is capitalizing the first letter of each word in a string. This Byte will explore three different ways we can achieve this: using the title(), capitalize(), and string.capwords() functions.

The title() Function

The title() function is a built-in method in Python that converts the first character of each word to uppercase and the remaining characters to lowercase. Here's how you can use it:

text = "welcome to stackabuse.com"
print(text.title())

The output will be:

Welcome To Stackabuse.Com

Note: The title() function capitalizes every word in a string, regardless of what the word is. This may not always be the desired behavior. For example, in our output, ".Com" is capitalized, which is not correct in terms of website domain naming conventions. So you may need to handle cases like this manually.

The capitalize() Function

The capitalize() function, on the other hand, only capitalizes the first letter of a string, while making all other characters in the string lowercase.

text = "welcome to STACKABUSE.COM"
print(text.capitalize())

This will output:

Welcome to stackabuse.com

As you can see, only the first letter of the string is capitalized, and all other letters are now lowercase.

The string.capwords() Function

The string.capwords() function from the string module is another way to capitalize the first letter of each word in a string. This function splits the string into words using whitespace, capitalizes the first letter of each word, and then joins them back together.

import string

text = "welcome to stackabuse.com"
print(string.capwords(text))
Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

The output will be:

Welcome To Stackabuse.com

You'll notice that in this case, ".com" is not capitalized like we saw with tite(). This is because it only splits on whitespace, so it considers "stackabuse.com" to be one word.

Example: Formatted Output in User Interfaces

Let's take a practical example to see how this works. Suppose we're making a user interface for a simple application. We have a form where the user can enter their full name. However, users can be unpredictable and might enter their name in all lower case, all upper case, or a mix of both. To ensure consistency in our application, we want to capitalize the first letter of each word in their name.

Here's how we can achieve this using the title() function:

def format_name(user_input):
    return user_input.title()

user_name = "jane doe"
formatted_name = format_name(user_name)
print(formatted_name)

When we run this script, the output will be:

$ Jane Doe

In this way, no matter how the user enters their name, it will always be formatted correctly in our application.

Although we're using title() in this example, you could also use string.capwords() if you prefer. Both will give you the same result in this case.

Note: While this is a toy example to show when and why you might title words, formatting names isn't actually this easy. There are names out there that don't actually start with a capital letter, like "Ludwig van Beethoven". It would technically incorrect to capitalize the "van". Unfortunately nothing is ever as easy as it seems in programming 😉

Conclusion

In this Byte, we've looked at three different Python functions that can be used to capitalize the first letter of each word in a string: title(), capitalize(), and string.capwords(). Each function has its own unique quirks and use-cases, but all of them can be useful when dealing with text data, depending on your use-case. Whether you're formatting user input in a UI, as in our example, or working with some kind of dataset, these functions can help format your data consistently.

Last Updated: August 31st, 2023
Was this helpful?

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms