bytes.title()

Headline case, where a "word" starts after ANY non-letter. That rule is what turns it's into It'S and makes title unsuitable for real names.

Bytes methodPython 3.0+Live demo
Common call
data.title()
Returns
a new bytes object; the original is unchanged
Replaces
splitting on spaces and capitalising each piece
Watch out
an apostrophe starts a new word: b"it's" becomes b"It'S"
bytes.title()
→ bytes

Demo

Live evaluation
Try:
Inputs
sstrdata (encoded as utf-8)
Output
bytes('hello world', 'utf-8').title()
b'Hello World'

A letter is uppercased whenever the byte before it is not a letter — so the start of the data, a space, a hyphen and a digit all begin a new word. That is why well-known becomes Well-Known and abc1def becomes Abc1Def. Everything else is lowercased, which is why uppercase input comes out as Hello World rather than staying shouted. The apostrophe case is the famous one: it's becomes It'S, because the apostrophe is not a letter.

Common patterns

Headline-case an ASCII label
Fine for simple space-separated words.
heading = raw.title()
Check whether data is already title case
istitle answers the question without rebuilding the buffer.
if data.istitle():
    ...
Title-case real text properly
Decode, then use string.capwords, which splits on whitespace only.
import string
string.capwords(data.decode('utf-8'))

Examples

1. Two words
b'hello world'.title()
Returns
b'Hello World'
2. Uppercase in
b'HELLO WORLD'.title()
Returns
b'Hello World'
3. Hyphen
b'well-known'.title()
Returns
b'Well-Known'
4. Digit boundary
b'abc1def'.title()
Returns
b'Abc1Def'
5. Apostrophe
b"it's".title()
Returns
b"It'S"
6. Empty
b''.title()
Returns
b''

Pitfalls

1. Apostrophes start new words
The classic failure. Because an apostrophe is not a letter, the letter after it is treated as the start of a word and uppercased, mangling contractions and possessives.
Mangled
b"it's".title()
b"It'S"
capwords on text
import string
string.capwords("it's")
"It's"
2. It lowercases everything else
Like capitalize, title flattens any existing capitals that are not at a word start. Acronyms and mixed-case names lose their casing.
Acronym flattened
b'NASA launch'.title()
b'Nasa Launch'
Handle it yourself
b' '.join(w if w.isupper() else w.title() for w in data.split())
keeps NASA
3. Non-ASCII bytes break words in the wrong place
Worse than being ignored: an accented letter is non-letter bytes to the bytes method, so it neither gets capitalised NOR counts as part of the word — the letter AFTER it is treated as a new word start and uppercased. élan becomes éLan.
Capital in the middle
'élan vital'.encode().title()
b'\xc3\xa9Lan Vital'
Work on text
'élan vital'.title().encode()
b'\xc3\x89lan Vital'

When to use

Use it
  • Headline-casing simple ASCII labels with plain spaces
  • Quick normalisation of consistently formatted input
Reach for something else
  • Anything with apostrophes, hyphens or digits inside words
  • Names and acronyms that must keep their own casing
  • Real text → decode and use string.capwords

Notes

Complexity
O(n) — one pass over the bytes
Return
A new bytes object of the same length
CPython impl
Objects/bytesobject.c :: stringlib_title
Memory
Allocates a buffer the same size as the input
Thread-safe
Yes — bytes are immutable

FAQ

Because title starts a new word after any byte that is not a letter, and an apostrophe is not a letter. The s is therefore the first letter of a "word" and gets uppercased. There is no option to change this rule; use string.capwords on decoded text instead.

import string
string.capwords("it's here")
# "It's Here"

History

3.0
bytes.title arrived with the bytes type in the text/binary split.