HOWTO · Python

How to Capitalize First Letter of Each Word in Python

Learn how to capitalize words in a Python string with str.title(), string.capwords(), or a custom split-and-join approach.

On this page

To capitalize words in a Python string, use str.title() for a quick title-style transformation, string.capwords() to split on whitespace and rejoin with single spaces, or str.split() with str.join() when you need custom handling for quotes or other prefixes. Choose based on whether punctuation and the original spacing need special treatment: the built-in title-casing functions can capitalize letters after apostrophes, while splitting and joining normalizes whitespace.

Capitalize Words with str.title()

Call str.title() on the string. It returns a new string, leaving the original unchanged, and keeps the input’s whitespace. Run this example:

text = "python string methods"
print(text.title())
Python String Methods

str.title() treats a letter after a non-letter character as the start of a new word. That means punctuation affects capitalization: for example, an apostrophe in what's makes the s a new word start, so title casing can produce What'S. This behavior is useful for title-style casing, but may not match ordinary English capitalization of contractions or possessives. It also capitalizes after punctuation such as an exclamation mark. The method preserves repeated spaces because it changes character case without splitting the string.

Capitalize Words with string.capwords()

string.capwords() is a function in Python’s string module, not a string method. With its default sep=None, it splits the text on runs of whitespace, applies str.capitalize() to each resulting word, and joins the words with one space. Import the module and call the function:

import string

text = "python   string methods"
print(string.capwords(text))
Python String Methods

This is convenient when collapsing whitespace is acceptable. Leading and trailing whitespace are removed, and tabs or line breaks used as separators also become ordinary single spaces. If you pass a separator, the function splits and rejoins using that exact separator instead; it does not preserve arbitrary spacing around the words. With the default behavior, a word surrounded by quotes remains quoted but its first character is still the quote, so the letter inside it is not capitalized. Passing an apostrophe as the separator is usually unsuitable: it separates contractions and can capitalize the letter after the apostrophe. Use this function when simple whitespace normalization and its punctuation behavior fit the input.

Capitalize Words with str.split() and str.join()

For custom behavior, use str.split() to obtain words, transform each word, and call str.join() on the separator to build the result. These are string methods: str.split() is the type-level name for text.split(), and str.join() describes calls such as " ".join(words). The following helper skips leading quotation marks and an opening parenthesis before capitalizing the first letter; it leaves the rest of each word unchanged:

def capitalize_word(word):
    prefix = ""
    while word and word[0] in "'\"(":
        prefix += word[0]
        word = word[1:]
    if not word:
        return prefix
    return prefix + word[0].upper() + word[1:]

text = "'hello'   what's up?"
words = text.split()
result = " ".join(capitalize_word(word) for word in words)
print(result)
'Hello' What's Up?

This approach capitalizes 'hello' as 'Hello' and keeps the s in what's lowercase because only the first letter of each whitespace-delimited word is changed. Extend the prefix rule if your data uses other opening characters, such as brackets. With no separator argument, str.split() treats consecutive whitespace as one delimiter and drops leading and trailing whitespace; joining with a single space therefore collapses repeated spaces and line breaks. This is a good tradeoff for normalized text, but not when the original spacing must remain exact. For casing that must follow language-specific rules, define the desired behavior explicitly rather than assuming these general-purpose methods handle every language’s word boundaries.