Last updated: Apr 8, 2024
Reading timeยท3 min
Use the len()
function to get the length of a bytes object.
The len()
function returns the length (the number of items) of an object and
can be passed a sequence (a bytes, string, list, tuple or range) or a collection
(a dictionary, set, or frozen set).
my_bytes = 'hello'.encode('utf-8') print(type(my_bytes)) # ๐๏ธ <class 'bytes'> print(len(my_bytes)) # ๐๏ธ 5
The len() function returns the length (the number of items) of an object.
Here is another example with some special characters.
my_bytes = 'รฉรฉ'.encode('utf-8') print(type(my_bytes)) # ๐๏ธ <class 'bytes'> print(len(my_bytes)) # ๐๏ธ 4
The same approach can be used to get the length of a bytearray
.
my_byte_array = bytearray('hello', encoding='utf-8') print(len(my_byte_array)) # ๐๏ธ 5
If you need to get the size of an object, use the sys.getsizeof()
method.
import sys my_bytes = 'hello'.encode('utf-8') print(sys.getsizeof(my_bytes)) # ๐๏ธ 38 print(sys.getsizeof('hello')) # ๐๏ธ 54
The sys.getsizeof() method returns the size of an object in bytes.
The object can be any type of object and all built-in objects return correct results.
The getsizeof
method only accounts for the direct memory consumption of the
object, not the memory consumption of objects it refers to.
The getsizeof()
method calls the __sizeof__
method of the object, so it
doesn't handle custom objects that don't implement it.
If you need to get the size of a string:
len()
function to get the number of characters in the string.sys.getsizeof()
method to get the size of the string in memory.string.encode()
method and len()
to get the size of the string in
bytes.import sys string = 'bobby' # โ Get the length of the string (number of characters) print(len(string)) # ๐๏ธ 5 # ------------------------------------------ # โ Get the memory size of the object in bytes print(sys.getsizeof(string)) # ๐๏ธ 54 # ------------------------------------------ # โ Get size of string in bytes my_bytes = len(string.encode('utf-8')) print(my_bytes) # ๐๏ธ 5 print(len('ลลดล'.encode('utf-8'))) # ๐๏ธ 6
Use the len()
function if you need to get the number of characters in a
string.
print(len('ab')) # ๐๏ธ 2 print(len('abc')) # ๐๏ธ 3
The len() function returns the length (the number of items) of an object.
The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).
sys.getsizeof()
method.import sys print(sys.getsizeof('bobby')) # ๐๏ธ 54 print(sys.getsizeof('a')) # ๐๏ธ 50
The sys.getsizeof() method returns the size of an object in bytes.
The object can be any type of object and all built-in objects return correct results.
The getsizeof
method only accounts for the direct memory consumption of the
object, not the memory consumption of objects it refers to.
The getsizeof()
method calls the __sizeof__
method of the object, so it
doesn't handle custom objects that don't implement it.
You can learn more about the related topics by checking out the following tutorials: