Python bytes.upper() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

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()
CallWhat 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 ValueDescription
bytesA 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)

How It Works

  1. bytes in a set. bytes objects are hashable, so they can be stored in a set for fast lookups.
  2. Mixed case fixed. b"Post" becomes b'POST' and is found in the set.
  3. Already uppercase. b"DELETE" is unchanged by upper() but is not an allowed method.

Example 2 — Show a Hex String in Capitals

hex() returns lowercase digits as a str. Encode that text to bytes and uppercase it, then try a token with digits and symbols.

python
data = b"\xde\xad\xbe\xef"
text = data.hex().encode("ascii")

print(text)
print(text.upper())
print(b"id-7f_ok".upper())

How It Works

  1. Hex as bytes. data.hex() is the str 'deadbeef'; encoding it gives ASCII bytes.
  2. Capital hex digits. upper() turns the letters a-f into A-F.
  3. Symbols unchanged. The digit 7, the hyphen, and the underscore pass straight through.

Example 3 — upper() and the German Sharp S

The str method turns the sharp s into two letters. The bytes method cannot change the length, so it leaves the encoded letter alone.

python
word = "stra\u00dfe"
data = word.encode("utf-8")

print(data.upper())
print(word.upper())
print(len(data), len(data.upper()))

How It Works

  1. Two untouched bytes. The sharp s is \xc3\x9f in UTF-8; both bytes are outside ASCII and stay lowercase.
  2. Unicode expansion. str.upper() follows Unicode rules and writes SS, so the text grows by one character.
  3. Same length. The bytes result has 7 bytes, exactly like the input.

Frequently Asked Questions

Only on its ASCII letters. Encoded non-ASCII letters are unchanged; decode to str for full Unicode uppercase.
No. It returns a new object.
Use data.isupper(), which is True when there is at least one ASCII letter and none of them are lowercase.
Yes, for ASCII data: a.upper() == b.upper().
A new bytearray with ASCII letters uppercased.

Next: bytes.zfill()

Pad on the left with ASCII zero bytes, keeping a leading sign in front.

zfill() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful