Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python memory usage for integer and string

Tags:

python

memory

I'm trying to understand the below snippet.

import sys

variable = 30
print(sys.getsizeof(variable)) #prints 28

If I change integer value with string

import sys

variable = "30"
print(sys.getsizeof(variable)) #prints 51

So does it means integer object makes use of less memory than string object.

Please let me know how Python makes use of memory.

like image 278
Arunkrishna Avatar asked Nov 05 '25 11:11

Arunkrishna


1 Answers

Yes integer object use 28 byte of memory while string object is getting bigger size of memory.

like image 58
Vahid Bahramian Avatar answered Nov 07 '25 01:11

Vahid Bahramian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!