Back to Blog
Python

Python String Title: Using str.title() Correctly

Learn how Python's str.title() method converts strings to title case, including word boundaries, apostrophe handling, Unicode behavior, and practical limitations.

str.title()title casestring methodstext formattingUnicode handling
Illustration of a Python string being converted to title case with the str.title() method, showing each word capitalized.

Python's str.title() method converts a string to title case: it returns a copy where each word begins with an uppercase letter and the remaining letters are lowercase. For example, "hello world".title() returns "Hello World". The method is built into every Python string and requires no imports.

How str.title() Determines Word Boundaries

The title() method does not split on spaces alone. It treats any sequence of characters that are not letters as a word boundary. This includes punctuation, digits, and whitespace. Consider:

text = "hello, world! how are you?" print(text.title()) # Hello, World! How Are You?

The comma, exclamation point, and spaces each create a boundary, so every word is capitalized independently. This differs from a naive split-on-space approach, which would leave punctuation attached to the preceding word and require additional handling.

What Happens with Apostrophes and Contractions

A common surprise is how title() handles apostrophes. An apostrophe is not a letter, so it creates a word boundary. This means the letter after the apostrophe is capitalized:

text = "don't stop" print(text.title()) # Don'T Stop

The output "Don'T" is rarely what you want. The method treats don and t as separate words because the apostrophe breaks the sequence of letters. If you are converting user input that contains contractions, title() will produce awkward results. There is no built-in parameter to change this behavior, so you would need a custom approach for such cases.

Numbers and Mixed Alphanumeric Strings

Digits also act as boundaries. A string like "version 2.0 released" becomes "Version 2.0 Released". The digit 2 and the period each break the sequence of letters, so 2 and 0 are not joined to neighboring text. In practice, title casing does not preserve camelCase or other mixed-format identifiers:

text = "userID and orderID" print(text.title()) # Userid And Orderid

The uppercase I and D in userID are converted to lowercase, producing Userid. If you need to preserve acronyms or mixed-case identifiers, title() is not the right tool.

Unicode and Non-English Text

The title() method uses Unicode letter categories. It capitalizes accented characters and letters from non-Latin scripts according to their Unicode case mappings. For example:

text = "élève français" print(text.title()) # Élève Français

The accented é is capitalized to É, and the lowercase è remains lowercase. This works because Python's string methods operate on Unicode code points rather than ASCII bytes. However, the apostrophe issue also applies to Unicode text: both the ASCII apostrophe (') and the right single quotation mark (') are non-letters, so they create word boundaries.

Comparing title(), capitalize(), and upper()

These three methods serve different purposes, and choosing the wrong one is a common mistake.

MethodBehaviorExample InputExample Output
title()Capitalizes first letter of each word"hello world""Hello World"
capitalize()Capitalizes first letter, lowercases the rest"hello world""Hello world"
upper()Uppercases every character"hello world""HELLO WORLD"

capitalize() is appropriate when you want only the first letter of the entire string capitalized, such as for a sentence. upper() is for all-caps output. title() is for headings, names, or display labels where each word should be capitalized.

When Title Case Is Appropriate in Real Code

Title casing is useful for display formatting, such as converting a slug or a database field into a human-readable heading. For example, a product name stored as "wireless mouse" can be displayed as "Wireless Mouse" in a UI. It is also useful for normalizing names in reports or logs where consistent capitalization matters.

However, title() is not appropriate for proper names that contain internal capitalization, such as "McDonald" or "iPhone". The method lowercases the internal uppercase letters, producing "Mcdonald" and "Iphone". If you are processing user-entered names, you should not rely on title() to produce correct proper nouns.

Runtime Behavior and Performance

The title() method creates a new string and iterates over the original once. Its time complexity is O(n), where n is the length of the input string. For typical display strings this is negligible. If you are calling title() on very large text repeatedly in a loop, the allocation cost of creating a new string on each call is the main consideration. Reusing the result instead of recomputing it for the same input avoids unnecessary allocation.

There is no in-place variant of title(). Strings are immutable in Python, so the method returns a new string rather than modifying the original.

Handling the Apostrophe Problem in Practice

If you need title casing that respects contractions, you can implement a small helper that uppercases the first letter of each word while leaving the rest of the word alone:

import re def smart_title(text): return re.sub(r"\b\w+(?:['’]\w+)*\b", lambda m: m.group(0)[0].upper() + m.group(0)[1:], text) print(smart_title("don't stop")) # Don't Stop

The regular expression treats contractions such as don't as one match, so the apostrophe does not cause the following letter to be capitalized. The helper leaves the rest of each matched word unchanged, which is different from title()'s behavior of lowercasing the remaining letters. This is a pragmatic workaround for English contractions, not a complete title-case rules engine. For example, smart_title("iPhone") returns "IPhone" because the internal capital is preserved; names and brand names may still need manual rules.

Python String Title: Practical str.title() Usage and Code Examples | RYUSLOG DEV