bytes.upper() returns a new bytes object in which each byte from a (97) to z (122) is replaced by its uppercase partner. Everything else, including digits, symbols, and bytes 128-255, is copied unchanged.
Use it to normalize ASCII tokens such as HTTP methods or command names before looking them up, or to print hex text in capitals. Because it works byte by byte, the length never changes.
bytes.upper() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.upper(...). No import is needed, and bytearray objects have the same method.
Syntax
No arguments; the uppercase copy is returned.
python
result = data.upper()
Call
What it does
b"get".upper()
Returns b'GET'.
b"id-7f".upper()
Returns b'ID-7F'.
bytearray(b"ok").upper()
Returns bytearray(b'OK').
Unlike str.upper(), the bytes version can never make the data longer.
Method signatures
python
bytes.upper()
# returns a new bytes object (bytearray.upper() returns a bytearray)
Parameters
upper() takes no parameters.
Return Value
Return Value
Description
bytes
A copy with ASCII lowercase letters converted to uppercase.
Python Examples
Example 1 — Normalize HTTP Methods Before a Lookup
Clients sometimes send methods in lowercase. Uppercase each one and check it against the set of methods the server allows.
python
methods = [b"get", b"Post", b"DELETE", b"patch"]
allowed = {b"GET", b"POST"}
for m in methods:
print(m.upper(), m.upper() in allowed)