Get the length of a Bytes object in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Table of Contents

  1. Get the length of a Bytes object in Python
  2. Get the size of a String in Python

# Get the length of a Bytes object in Python

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).

main.py
my_bytes = 'hello'.encode('utf-8') print(type(my_bytes)) # ๐Ÿ‘‰๏ธ <class 'bytes'> print(len(my_bytes)) # ๐Ÿ‘‰๏ธ 5

get length of bytes

The code for this article is available on GitHub

The len() function returns the length (the number of items) of an object.

The example shows how to get the number of bytes in the bytes object.

Here is another example with some special characters.

main.py
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.

main.py
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.

main.py
import sys my_bytes = 'hello'.encode('utf-8') print(sys.getsizeof(my_bytes)) # ๐Ÿ‘‰๏ธ 38 print(sys.getsizeof('hello')) # ๐Ÿ‘‰๏ธ 54
The code for this article is available on GitHub

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 size of an object includes the data stored by the object, the attributes, the methods, etc.

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.

# Get the size of a String in Python

If you need to get the size of a string:

  1. Use the len() function to get the number of characters in the string.
  2. Use the sys.getsizeof() method to get the size of the string in memory.
  3. Use the string.encode() method and len() to get the size of the string in bytes.
main.py
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

get size of string

The code for this article is available on GitHub

Use the len() function if you need to get the number of characters in a string.

main.py
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).

If you need to get the memory consumption of a string in bytes, use the sys.getsizeof() method.
main.py
import sys print(sys.getsizeof('bobby')) # ๐Ÿ‘‰๏ธ 54 print(sys.getsizeof('a')) # ๐Ÿ‘‰๏ธ 50

using sys getsizeof method

The code for this article is available on GitHub

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 size of an object includes the data stored by the object, the attributes, the methods, etc.

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev