all Python 3Byte sequencesfunction
bytearray.capitalize
bytearray — case
Return a copy with the first byte upper-cased and the rest lower-cased.
Syntax
ba.capitalize(...)
Only ASCII letters change case: bytes 0x80-0xFF, such as UTF-8 encoded Cyrillic, pass through unchanged because a bytearray knows nothing about encodings. The other surprise is that everything after the first byte is forced to lower case, so b'iPhone' becomes b'Iphone'; to preserve internal capitals, upper-case the first byte yourself and concatenate it with the untouched remainder.
Examples
print(bytearray(b'hello').capitalize()) # → bytearray(b'Hello')
print(bytearray(b'HELLO WORLD').capitalize()) # → bytearray(b'Hello world')
names = [bytearray(b'ANNA'), bytearray(b'bob')]; print([n.capitalize().decode() for n in names]) # → ['Anna', 'Bob']
ba = bytearray(b'python'); print(ba.capitalize(), ba) # → bytearray(b'Python') bytearray(b'python')
print(bytearray(b'123abc').capitalize()) # → bytearray(b'123abc')
print(bytearray(b'').capitalize()) # → bytearray(b'')
See also
Open in the full glossarydocs.python.orgReport an inaccuracy