Python | Size of string in memory
Last Updated :
12 Apr, 2023
While working with strings, sometimes, we require to get the size of the string, i.e the length of it. But there can be situations in which we require to get the size that the string takes in bytes usually useful in case one is working with files. Let’s discuss certain ways in which this can be performed.
Method #1 : Using len() + encode() The simplest and initial method that comes to the mind is to convert the string into a byte format and then extract its size.
Python3
test_string = "geekforgeeks"
print ("The original string : " + str (test_string))
res = len (test_string.encode( 'utf-8' ))
print ("The length of string in bytes : " + str (res))
|
Output :
The original string : geekforgeeks
The length of string in bytes : 12
Method #2 : Using sys.getsizeof() This task can also be performed by one of the system calls, offered by Python as in sys function library, the getsizeof function can get us the size in bytes of desired string.
Python3
import sys
test_string = "geekforgeeks"
print ("The original string : " + str (test_string))
res = sys.getsizeof(test_string)
print ("The length of string in bytes : " + str (res))
|
Output :
The original string : geekforgeeks
The length of string in bytes : 12
Method #3 : Using the memoryview function:
To get the number of bytes in a string using a memoryview, you can use the nbytes attribute of the memoryview object. This attribute returns the total size of the memoryview’s data in bytes.
Python3
test_string = "geekforgeeks"
print ( "The original string : " + str (test_string))
res = memoryview(test_string.encode( 'utf-8' )).nbytes
print ( "The length of string in bytes : " + str (res))
|
Output
The original string : geekforgeeks
The length of string in bytes : 12
METHOD 4:Using array method
APPROACH:
This code first calculates the size of the string in bytes using the len() function and the encode() method. Then, it creates an array of bytes from the string using the array module. Finally, it calculates the size of the array in bytes and prints both the size in bytes using len() and array.
ALGORITHM:
1. Take the input string from the user.
2. Calculate the size of the string in bytes using the len() function and the encode() method.
3. Create an array of bytes from the string using the array module.
4. Calculate the size of the array in bytes by multiplying the itemsize of the array by the length of the array.
5. Print the size of the string in bytes using len() and the size of the array in bytes.
Python3
import array
string = "geekforgeeks"
bytes_size = len (string.encode( 'utf-8' ))
arr = array.array( 'B' , string.encode( 'utf-8' ))
size = arr.itemsize * len (arr)
print (f "The size of the string '{string}' in bytes is {bytes_size} using len() and {size} using array." )
|
Output
The size of the string 'geekforgeeks' in bytes is 12 using len() and 12 using array.
The overall time complexity of the algorithm is O(n).
The overall space complexity of the algorithm is O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...