How to Get String and Bytes Length and Size in Python
This guide explores how to determine the length and memory size of string and bytes objects in Python. We'll cover using the len()
function for character count, and the sys.getsizeof()
method for memory size, as well as how to get a string's size in bytes by using the encoding.
Getting the Length of a Bytes Object
Use the len()
function to get the number of bytes in a bytes object:
my_bytes = 'hello'.encode('utf-8')
print(type(my_bytes)) # Output: <class 'bytes'>
print(len(my_bytes)) # Output: 5
- The
encode('utf-8')
method converts the string to a bytes object using the UTF-8 encoding. - The
len()
function returns the number of bytes in the object.
Note that characters outside of the ASCII range can take multiple bytes in UTF-8, as shown in the example below:
my_bytes = 'éé'.encode('utf-8')
print(len(my_bytes)) # Output: 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)) # Output: 5
If you need to get the memory size of a bytes object, use the sys.getsizeof()
method:
import sys
my_bytes = 'hello'.encode('utf-8')
print(sys.getsizeof(my_bytes)) # Output: 38
print(sys.getsizeof('hello')) # Output: 54
- The
sys.getsizeof()
method returns the number of bytes occupied by the object in memory.
The output of sys.getsizeof()
will vary based on the system used.
Getting the Size of a String
To understand the size of a string, you may need to understand its length in characters, memory consumption, or size in bytes using a specific encoding.
Using len()
for Character Count
The len()
function directly returns the number of characters in a string:
string = 'hanna'
print(len(string)) # Output: 5
print(len('ab')) # Output: 2
print(len('abc')) # Output: 3
- This is useful if you need to know how many characters there are in a string.
Using sys.getsizeof()
for Memory Consumption
To get the size of a string object in memory (including the object overhead and the data), use the sys.getsizeof()
method:
import sys
string = 'hanna'
print(sys.getsizeof(string)) # Output: 54
print(sys.getsizeof('a')) # Output: 50
- This method provides you with information about how much memory the string takes up in bytes. Note that this may vary depending on your system and Python version.
Getting Size in Bytes with encode()
To get the number of bytes required to store a string using a specific encoding (e.g. UTF-8), encode the string to a bytes object using encode()
, and then use len()
:
string = 'hanna'
my_bytes = len(string.encode('utf-8'))
print(my_bytes) # Output: 5
print(len('őŴœ'.encode('utf-8'))) # Output: 6
- Encoding your string with
encode('utf-8')
returns abytes
object which length is equal to the amount of memory the string occupies in bytes using UTF-8 encoding.