bytes.lower() returns a new bytes object where each byte from A (65) to Z (90) is replaced by its lowercase partner; every other byte is copied as it is.
It is the usual first step for case-insensitive comparisons of ASCII protocol data: commands typed into a socket, header names, or hex digits. Bytes from 128 to 255 are never changed, even if they encode uppercase letters in some text encoding.
bytes.lower() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.lower(...). No import is needed, and bytearray objects have the same method.
Syntax
No arguments; assign or compare the returned copy.
python
result = data.lower()
Call
What it does
b"PING".lower()
Returns b'ping'.
b"MAC=AA:BB".lower()
Returns b'mac=aa:bb'.
b"\xc4BC".lower()
Returns b'\xc4bc': the non-ASCII byte is unchanged.
The result always has the same length as the input.
Method signatures
python
bytes.lower()
# returns a new bytes object (bytearray.lower() returns a bytearray)
Parameters
lower() takes no parameters.
Return Value
Return Value
Description
bytes
A copy with ASCII uppercase letters converted to lowercase.
Python Examples
Example 1 — Match Commands in Any Case
A server receives commands typed by people in mixed case, some with a trailing CRLF. Strip and lower each one before comparing.
python
commands = [b"PING", b"Quit", b"help", b"STATUS\r\n"]
for raw in commands:
cmd = raw.strip().lower()
print(cmd, cmd == b"quit")