bytes.capitalize() returns a new bytes object in which the first byte is converted to uppercase and every other byte to lowercase. Only the ASCII letters a-z and A-Z are affected.
Digits, punctuation, control bytes, and every byte from 128 to 255 are copied unchanged. That means UTF-8 encoded letters such as \xc3\xa9 (e with acute accent) are never capitalized; decode to str first when you need Unicode case rules.
bytes.capitalize() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.capitalize(...). No import is needed, and bytearray objects have the same method.
Syntax
Call it with no arguments and keep the returned copy.
python
result = data.capitalize()
Call
What it does
b"hello WORLD".capitalize()
Returns b'Hello world'.
b"9lives".capitalize()
Returns b'9lives': the first byte is not a letter.
bytearray(b"ok").capitalize()
Returns bytearray(b'Ok').
Only the very first byte can become uppercase; a leading space or digit means no byte is uppercased at all.
Method signatures
python
bytes.capitalize()
# returns a new bytes object (bytearray.capitalize() returns a bytearray)
Parameters
capitalize() takes no parameters.
Return Value
Return Value
Description
bytes
A copy with the first byte uppercased and the remaining ASCII letters lowercased.
Python Examples
Example 1 — Normalize Protocol Keywords
Capitalize a few tokens received in mixed case, including one that starts with a digit and one that starts with a non-ASCII byte.
python
words = [b"hELLO", b"content-TYPE", b"9lives", b"\xe9t\xe9"]
for w in words:
print(w.capitalize())
Output
b'Hello'
b'Content-type'
b'9lives'
b'\xe9t\xe9'
How It Works
First up, rest down.b"hELLO" becomes b'Hello': H is raised and ELLO is lowered.
One word only. The hyphen does not start a new word, so TYPE is lowercased to type.
Digit first. Nothing in b"9lives" needs to change.
Latin-1 byte untouched.\xe9 is outside ASCII, so it is neither a letter nor uppercased.
Example 2 — capitalize() Compared with title()
Both methods raise a first letter, but they disagree about the rest of the bytes. The original stays as it was.
python
cmd = b"quit NOW please"
print(cmd.capitalize())
print(cmd.title())
print(cmd)
Output
b'Quit now please'
b'Quit Now Please'
b'quit NOW please'
How It Works
Sentence style.capitalize() treats the whole object as one sentence: only the first byte is uppercase.
Word style.title() raises the first letter of every word instead.
Immutable source. Printing cmd again shows that neither call changed it.
Example 3 — Why UTF-8 Text Is Not Capitalized
Encode a French word that starts with an accented letter, then compare the bytes method with the str method.
python
text = "\u00e9cole"
data = text.encode("utf-8")
print(data.capitalize())
print(ascii(data.decode("utf-8").capitalize()))
print(ascii(data.capitalize().decode("utf-8")))
Output
b'\xc3\xa9cole'
'\xc9cole'
'\xe9cole'
How It Works
Two-byte letter. The accented e is encoded as \xc3\xa9; the first byte \xc3 is not an ASCII letter, so nothing is raised.
str knows Unicode. Decoding first lets str.capitalize() turn U+00E9 into U+00C9, which ascii() prints as \xc9.
Bytes result decoded. Decoding the unchanged bytes gives back the lowercase word, confirming the bytes method did nothing.
Frequently Asked Questions
It returns a copy with the first byte uppercased and all other ASCII letters lowercased. Non-letters and non-ASCII bytes are unchanged.
Its first letter is stored as two non-ASCII bytes, and bytes methods only know ASCII letters. Decode, call str.capitalize(), then encode again.
No. bytes are immutable, so you must use the returned object.
capitalize() raises only the first byte of the whole object; title() raises the first letter of every word.
Yes, and it returns a new bytearray rather than modifying the original.