🌐
Python Bytes
pythonbytes.fm
Python Bytes | The #1 Python News Podcast
Python Bytes is a weekly podcast hosted by Michael Kennedy and Calvin Hendryx-Parker.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-bytes-method
Python bytes() method - GeeksforGeeks
July 11, 2025 - By passing a single integer to bytes(), Python will create a bytes object of that length, filled with zero values (because a byte is initially 0).
🌐
Programiz
programiz.com › python-programming › methods › built-in › bytes
Python bytes()
Online Python Online JavaScript ... Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The bytes() method returns an immutable bytes object initialized with the given size and data....
🌐
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 ...
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.7 documentation
For performance reasons, the value of errors is not checked for validity unless an encoding error actually occurs, Python Development Mode is enabled or a debug build is used. For example: >>> encoded_str_to_bytes = 'Python'.encode() >>> type(encoded_str_to_bytes) <class 'bytes'> >>> encoded_str_to_bytes b'Python'
🌐
W3Schools
w3schools.com › python › ref_func_bytes.asp
Python bytes() Function
It can convert objects into bytes objects, or create empty bytes object of the specified size.
🌐
Python
docs.python.org › 3 › c-api › bytes.html
Bytes Objects — Python 3.14.7 documentation
If v is NULL, the contents of the bytes object are uninitialized. PyObject *PyBytes_FromFormat(const char *format, ...)¶ · Return value: New reference. Part of the Stable ABI. Thread safety: Atomic. Take a C printf()-style format string and a variable number of arguments, calculate the size of the resulting Python bytes object and return a bytes object with the values formatted into it.
🌐
Mimo
mimo.org › glossary › python › bytes
Python Bytes: Syntax, Usage, and Examples
For instance, compression libraries often accept or return byte arrays. Python bytes provide a fast and reliable way to handle binary data. You can convert between strings and bytes easily using .encode() and .decode(). When you need mutability, use bytearray.
Find elsewhere
🌐
w3resource
w3resource.com › python › python-bytes.php
Python Bytes, Bytearray
Bytes objects can be constructed the constructor, bytes(), and from literals; use a b prefix with normal string syntax: b'python'. To construct byte arrays, use the bytearray() function.
🌐
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 ...
🌐
Real Python
realpython.com › python-bytes
Bytes Objects: Handling Binary Data in Python – Real Python
March 5, 2025 - The difference between bytes and bytearray is that bytes objects are read-only, while bytearray objects are mutable. You convert a Python string to bytes using the str.encode() method, the bytes() function, or the codecs module.
🌐
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.
🌐
Scaler
scaler.com › home › topics › python bytes() function
Python bytes() Function - Scaler Topics
January 2, 2024 - The Python bytes() Function returns a bytes object. It can convert objects into bytes objects or create empty bytes objects of the specified size.
🌐
Python-future
python-future.org › bytes_object.html
bytes — Python-Future documentation
Here is an example that works identically on Python 2.x and 3.x: >>> from builtins import bytes >>> b = bytes(b'\xff') >>> b.decode('utf-8', 'surrogateescape') '\udcc3'
🌐
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
🌐
Reddit
reddit.com › r/learnpython › i don't understand python bytes objects, its purpose and when to use it?
r/learnpython on Reddit: I don't understand Python bytes objects, its purpose and when to use it?
June 11, 2020 -

I am haveing some hard time here trying to understand Python's bytes object. Why it was intruduced, when to use it? and Why to use it? Why older python didn't have bytes object, and things were kind ok, and they came up with the ideas to invent bytes object?

Appraciate if someone could point to an artice/video where bytes object is explaned like if I am five years old.

Thanks.

Top answer
1 of 2
13
bytes objects are used whenever you need to deal with some sort of data you don't necessarily understand. For example, let's say you're opening a network connection. What do you send over that connection? You send data in some form, and that data doesn't have to be text - it could be, but it could also be a more complicated protocol with its own data format like, say, HTTP/2 or SSH. How do you represent that data in Python? You can't use str, because the data might not be text, or even representable as text. You could use a list of numbers, but that's really inefficent. Thus, bytes.
2 of 2
5
A chunk of bytes is just raw memory or storage contents - zeroes and ones collected into groups of eight bits, and it doesn't have any specific meaning until you decide how to interpret it. If I have four bytes, what do they mean? Are they ASCII characters, UNICODE codepoints, or an integer? If they're an integer, are they big-endian or little-endian? What if your program doesn't need to know - if it's just shifting the bytes from one place to another, it doesn't matter. Networks and files are of this nature - the data could be anything, so if you don't have to know what's in there, it's not worth checking. The operating system thus usually makes no assumptions as to the type of data held within the bytes when transporting it over the network or to and from storage. I'm afraid this explanation has got pretty long, but I've tried to cover enough background to explain the main concepts and drivers behind why we now have the bytes type. The TL;DR is "we have the bytes type because it was necessary to get around problems caused by assumptions in code that would cause difficult-to-debug problems when those assumptions were not met; treating data of an unknown encoding as raw bytes gives you explicit errors that force you to deal with the unknowns rather than just hoping for the best". So, if some chunk of bytes is supposed to be text, you need to know how it's encoded - is it ASCII, UNICODE or EBCDIC? If you guess and treat it as ASCII when it's EBCDIC, you will get gibberish, and vice-versa. Old Python, and lots of other languages of the time treated raw data as strings, and this ended up causing a lot of problems in corner cases, especially to do with networking and file I/O. Lots of libraries accepted strings as input, and assumed that they contained ASCII data, or other textual encodings, and failed in unexpected ways when that assumption was incorrect. Then, as the internet became ubiquitous around the world, extended character encodings became important. Standard ASCII, the encoding used at the time, represents each character with a 7-bit byte, so it only has 127 code points, though you can go up to 255 if you use extended 8-bit versions, so it doesn't have an easy way to display all the characters and punctuation common in non-English speaking languages. In the early 90's, you used to select whichever codepage was best for your language, but in doing so, you often couldn't display characters from other languages, which was bad if you wanted to work internationally. Imagine if Facebook only worked with the standard American symbol set - no accents, no other currency symbols, no Chinese, or other non-alphabetical texts... it wouldn't have ever got popular outside of the US. So Unicode became a thing, which seeks to encode all character sets eventually. It's based on ASCII, but adds the ability to represent character points which are composed of more than one byte, thus, if you try to print out Unicode as ASCII, you will end up printing garbage whenever you hit a "long character". To complicate things further, there are multiple ways of storing Unicode - variable length, fixed-length in various sizes. You really need to know what you're dealing with to process it properly. In the end, a decision was made to transition Python over to support Unicode properly, and in order to prevent functions that accepted a string, and expected it to have a specific encoding choking on data that didn't match that expectation, a new raw data concept - bytes - was introduced. Now if you try to feed binary data into a function that expects a string, it will cause an error, rather than subtly screwing up because the data doesn't match its expectations.
🌐
Wikiversity
en.wikiversity.org › wiki › Python_Concepts › Bytes_objects_and_Bytearrays
Python Concepts/Bytes objects and Bytearrays - Wikiversity
January 22, 2025 - Retrieved from "https://en.wikiversity.org/w/index.php?title=Python_Concepts/Bytes_objects_and_Bytearrays&oldid=2696525" Category: Python · Search · Python Concepts/Bytes objects and Bytearrays ·
🌐
Real Python
realpython.com › python-bytearray
Python's Bytearray: A Mutable Sequence of Bytes – Real Python
July 18, 2026 - You can create a bytearray using the bytearray() constructor with various arguments or from a string of hexadecimal digits using .fromhex(). This tutorial explores creating, modifying, and using bytearray objects in Python.
🌐
Tutorialspoint
tutorialspoint.com › python › python_bytes_function.htm
Python bytes() Function
The Python bytes() function returns a new bytes object of the specified size.
🌐
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 - An integer? What’s going on? This isn’t a bug; it’s a feature. It’s the key that unlocks a fundamental concept in modern Python: a bytes object is not a string of characters.