Python bytes.endswith() Method

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

Definition

bytes.endswith() returns True if the bytes end with suffix, or with any suffix in a tuple, and False otherwise.

Typical uses: telling CRLF (b"\r\n") from LF (b"\n") line endings, checking that a message carries its terminator, and filtering file names returned as bytes by their extension. start and end narrow the check to a slice, for example to ignore a trailing checksum byte.

bytes.endswith() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.endswith(...). No import is needed, and bytearray objects have the same method.

Syntax

Pass a suffix or a tuple of suffixes, plus an optional range.

python
ok = data.endswith(suffix)
ok = data.endswith((suffix1, suffix2))
ok = data.endswith(suffix, start, end)
CallWhat it does
b"line\r\n".endswith(b"\r\n")Returns True.
b"a.mp3".endswith((b".mp3", b".ogg"))Returns True.
b"END!".endswith(b"END", 0, 3)Returns True: only b"END" is checked.
b"abc".endswith(b"C")Returns False: matching is case-sensitive.

Like startswith(), the suffix must be bytes-like or a tuple of bytes-like objects; an int is not accepted.

Method signatures
python
bytes.endswith(suffix[, start[, end]])
# suffix: bytes-like object or tuple of bytes-like objects

Parameters

ParameterDescription
suffixA bytes-like object, or a tuple of them.
startOptional. Index where the checked slice begins.
endOptional. Index where the checked slice ends; the suffix must end right before it.

Return Value

Return ValueDescription
Truedata[start:end] ends with the suffix (or one of the tuple items).
FalseIt does not.

Python Examples

Example 1 — Tell CRLF, LF, and Missing Line Endings Apart

Lines read from a file opened in binary mode keep their line endings. Check the longer ending first so CRLF lines are not reported as LF.

python
lines = [b"alpha\r\n", b"beta\n", b"gamma"]

for raw in lines:
    if raw.endswith(b"\r\n"):
        print(raw, "CRLF")
    elif raw.endswith(b"\n"):
        print(raw, "LF")
    else:
        print(raw, "no newline")

How It Works

  1. Longest first. b"alpha\r\n" also ends with b"\n", so the CRLF test must come before the LF test.
  2. Unix ending. b"beta\n" fails the first test and passes the second.
  3. Last line. The final line of a file often has no newline at all, which the else branch reports.

Example 2 — Filter Media Files by Extension

File names can arrive as bytes, for example from os.listdir(b"."). A tuple checks several extensions, and lowering first ignores case.

python
names = [b"song.mp3", b"clip.MP4", b"notes.txt", b"movie.mkv"]
media = (b".mp3", b".mp4", b".mkv")

for name in names:
    print(name, name.endswith(media), name.lower().endswith(media))

How It Works

  1. One call, three suffixes. The tuple media is reused for every name.
  2. Case-sensitive by default. b"clip.MP4" fails the plain check because .MP4 is not .mp4.
  3. ASCII lowercase. lower() turns the name into b"clip.mp4", which matches.

Example 3 — Check a Terminator Before a Checksum Byte

This frame ends with a one-byte checksum after the END marker. Use end to look at the bytes before the checksum.

python
frame = b"DATA;END\x5a"

print(frame.endswith(b"END"))
print(frame.endswith(b"END", 0, len(frame) - 1))
print(frame.endswith(b";END", 4, 8))
print(frame[-1], hex(frame[-1]))

How It Works

  1. Checksum in the way. The last byte is the checksum 0x5a, so the frame itself does not end with END.
  2. Drop the last byte. With end=len(frame) - 1 the checked slice is b'DATA;END', which does end with the marker.
  3. Exact window. frame[4:8] is b';END', so a check with both bounds succeeds.
  4. Inspect the checksum. The last byte is the int 90; hex() shows it as 0x5a.

Frequently Asked Questions

Use data.endswith(b"\n"). To accept both CRLF and LF, data.endswith(b"\n") is enough, and data.endswith(b"\r\n") tells you if it was CRLF.
Yes, as a tuple: name.endswith((b".jpg", b".png")).
Lowercase the data first: name.lower().endswith(b".jpg"). Only ASCII letters are affected.
They select the slice data[start:end]; endswith() checks whether that slice ends with the suffix.
Use removesuffix() on Python 3.9+, or slice: data[:-len(suffix)] after endswith() returned True.

Next: bytes.expandtabs()

Replace tab bytes with spaces up to the next tab stop.

expandtabs() →

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