Possible duplicate of what is the difference between a string and a byte string

In short, the bytes type is a sequence of bytes that have been encoded and are ready to be stored in memory/disk. There are many types of encodings (utf-8, utf-16, windows-1255), which all handle the bytes differently. The bytes object can be decoded into a str type.

The str type is a sequence of unicode characters. The str needs to be encoded to be stored, but is mutable and an abstraction of the bytes logic.

There is a strong relationship between str and bytes. bytes can be decoded into a str, and strs can be encoded into bytes.

You typically only have to use bytes when you encounter a string in the wild with a unique encoding, or when a library requires it. str , especially in python3, will handle the rest.

More reading here and here

Answer from Jtcruthers on Stack Overflow
🌐
Real Python
realpython.com › python-bytes
Bytes Objects: Handling Binary Data in Python – Real Python
March 5, 2025 - Since bytes are closely related ... you’ll understand that: Python bytes objects are immutable sequences of unsigned bytes used for handling binary data....
🌐
Python
docs.python.org › 3 › c-api › bytes.html
Bytes Objects — Python 3.14.7 documentation
This instance of PyTypeObject represents the Python bytes type; it is the same object as bytes in the Python layer.
🌐
Real Python
realpython.com › ref › builtin-types › bytes
bytes | Python’s Built-in Data Types – Real Python
The built-in bytes data type allows you to represent and manipulate immutable sequences of bytes, which are numbers in the range 0 <= x < 256.
🌐
Mimo
mimo.org › glossary › python › bytes
Python Bytes: Syntax, Usage, and Examples
The bytes type in Python represents a sequence of immutable byte values ranging from 0 to 255.
🌐
Programiz
programiz.com › python-programming › methods › built-in › bytes
Python bytes()
# convert string to bytes byte_message = bytes(message, 'utf-8') print(byte_message) # Output: b'Python is fun' ... bytes() method returns a bytes object which is an immutable (cannot be modified) sequence of integers in the range 0 <=x < 256.
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.7 documentation
While the in and not in operations are used only for simple containment testing in the general case, some specialised sequences (such as str, bytes and bytearray) also use them for subsequence testing: ... Values of n less than 0 are treated as 0 (which yields an empty sequence of the same type as s).
🌐
The Python Coding Stack
thepythoncodingstack.com › the python coding stack › `bytes`: the lesser-known python built-in sequence • and understanding utf-8 encoding
`bytes`: The Lesser-Known Python Built-In Sequence • And Understanding UTF-8 Encoding
July 15, 2025 - The first three bytes represent the first three characters C, a, and f. These are single bytes containing the characters' ASCII codes, which each fit within a single byte. The last two bytes combined represent the last character, é. ... Do ...
Find elsewhere
🌐
w3resource
w3resource.com › python › python-bytes.php
Python Bytes, Bytearray
Python supports a range of types to store sequences. There are six sequence types: strings, byte sequences (bytes objects), byte arrays (bytearray objects), lists, tuples, and range objects.
🌐
ZetCode
zetcode.com › python › bytes-type
Python bytes - using bytes type in Python
When we open network sockets, work with serial I/O or open binary files, we work with the bytes type. Python has a mutable equivalent of the bytes type called bytearray.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-bytes-method
Python bytes() method - GeeksforGeeks
July 11, 2025 - return type of the bytes() method is a bytes object. A bytes object is an immutable sequence of integers in the range from 0 to 255. If the string contains characters from other languages, we can specify a different encoding: ... a = ...
🌐
iO Flood
ioflood.com › blog › python-bytes
Python Bytes Data Type | bytes() Function Guide
February 6, 2024 - As you continue to work with bytes in Python, you may find that the basic bytes() function doesn’t always meet your needs. Fortunately, Python provides several other ways to handle bytes, including the bytearray type.
🌐
W3Schools
w3schools.com › python › ref_func_bytes.asp
Python bytes() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The bytes() function returns a bytes object.
🌐
Reddit
reddit.com › r/learnpython › coming from c, how to better understand python's byte data type?
r/learnpython on Reddit: Coming from C, how to better understand Python's byte data type?
September 3, 2022 -

I'm reading the official docs and all, tried some examples, but still feels like it's just a string.

Then I didn't understand why when I loop through a bytes object and print the current element it comes out as an integer, but when I print the whole content of the object it comes out as string like: b'whatever is in the string', and when I cast something using bytes() it returns a string formatted as hex.

Top answer
1 of 2
10
bytes is an array of uint8's. str is an array of uint32's, but only those that are valid Unicode values. How they iterate or display by default is unrelated to the type of data they store. The python gods decided that iterating over a bytes type should return int objects, while iterating over a str object should return length 1 strings. They also decided that the default display of bytes should be to render all visible ascii characters as ascii, and the rest as escaped hex. They made these decisions presumably due to how these datatypes are usually used. For example bytes is used for low level communication a lot, which often is human readable.
2 of 2
5
text = '🐘 😊' print(f'{text!r} - {len(text)=}') # '🐘 😊' - len(text)=3 data = text.encode() print(f'{data!r} - {len(data)=}') # b'\xf0\x9f\x90\x98 \xf0\x9f\x98\x8a' - len(data)=9 A str is a sequence of characters (unicode codepoint), but a bytes is a sequence of... bytes. Unsigned integers in range 0-255. One can obtain bytes by encoding a str, or by ASCII code literal (as you do), or from other methods like sockets, binary file IO, structure packing, or various others. import struct data = struct.pack('!3h', 20, 21, 22) print(f'{data!r} - {len(data)=}') # b'\x00\x14\x00\x15\x00\x16' - len(data)=6 tup = struct.unpack('!3h', b'\x00\x17\x00\x18\x00\x19') print(f'{tup!r}') # (23, 24, 25) The byte output format is to show bytes as ascii characters, or the hex if there is no ascii character for that byte. You can force hex representation with data.hex() https://docs.python.org/3/library/stdtypes.html#bytes.hex or fetch all the integer values by putting them into a list list(data)
🌐
AskPython
askpython.com › python › built-in-methods › python-bytes
Python bytes() - AskPython
February 16, 2023 - Python bytes() is a built-in function which returns a bytes object that is an immutable sequence of integers in the range 0 <= x < 256. Depending on the type of object passed as the source, it initializes the byte object accordingly.
Top answer
1 of 3
54

The new bytes type is 3.x only. The 2.x bytes built-in is just an alias to the str type. There is no new type called bytes in 2.x; Just a new alias and literal syntax for str.

Here's the documentation snippet everybody loves:

Python 2.6 adds bytes as a synonym for the str type, and it also supports the b'' notation.

The 2.6 str differs from 3.0’s bytes type in various ways; most notably, the constructor is completely different. In 3.0, bytes([65, 66, 67]) is 3 elements long, containing the bytes representing ABC; in 2.6, bytes([65, 66, 67]) returns the 12-byte string representing the str() of the list.

The primary use of bytes in 2.6 will be to write tests of object type such as isinstance(x, bytes). This will help the 2to3 converter, which can’t tell whether 2.x code intends strings to contain either characters or 8-bit bytes; you can now use either bytes or str to represent your intention exactly, and the resulting code will also be correct in Python 3.0.

2 of 3
41

The bytes type was introduced in Python 3, but what's being discussed in the PEP is a mutable sequence (bytes is immutable) which was introduced in Python 2.6 under the name bytearray.

The PEP clearly wasn't implemented as stated (and it does say that it was partially superseded by PEP 3137) but I think it's only a question of things being renamed, not features missing. In Python 2 bytes is just an alias for str to aid forward compatibility and so is a red-herring here.

Example bytearray usage:

>>> a = bytearray([1,2,3])
>>> a[0] = 5
>>> a
bytearray(b'\x05\x02\x03')
🌐
Wikiversity
en.wikiversity.org › wiki › Python_Concepts › Bytes_objects_and_Bytearrays
Python Concepts/Bytes objects and Bytearrays - Wikiversity
January 22, 2025 - One byte is a memory location with a size of 8 bits. A bytes object is an immutable sequence of bytes, conceptually similar to a string · Because each byte must fit into 8 bits, each member x of a bytes object is an unsigned int that satisfies 0≤x≤0b1111_1111
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › built-in › bytes › python-bytes
Python bytes() function | Why is Python bytes() function used? |
July 30, 2021 - Python bytes() function is used to convert an object to an immutable (cannot be modified) byte object of the given size and data. The Python bytes() function returns a byte’s object, which is an immutable series of integer numbers ranging from 0 to 256. Python bytes() method is to manipulate ...
🌐
Medium
medium.com › @tihomir.manushev › more-than-a-string-a-deep-dive-into-pythons-bytes-and-bytearray-735e0905f001
More Than a String: A Deep Dive into Python’s bytes and bytearray | by Tihomir Manushev | Medium
November 5, 2025 - If you’ve ever been baffled by a Python error message, you’re not alone. But some are more baffling than others. Consider this seemingly simple piece of code: # A simple bytes object binary_greeting = b'hello' # Let's get the first character... or so we think. first_item = binary_greeting[0] print(f"The first item is: {first_item}") print(f"Its type is: {type(first_item)}")
🌐
Python-future
python-future.org › bytes_object.html
bytes — Python-Future documentation
On Py2, this object is a subclass of Python 2’s str that enforces the same strict separation of unicode strings and byte strings as Python 3’s bytes object: >>> b + u'EFGH' # TypeError Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: argument can't be unicode string >>> bytes(b',').join([u'Fred', u'Bill']) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: sequence item 0: expected bytes, found unicode string >>> b == u'ABCD' False >>> b < u'abc' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: bytes() and <type 'unicode'>