How to get the size of a string in Python? -
for example, string:
str = "please answer question"
i want write file.
but need know size of string before writing string file. function can use calculate size of string?
if talking length of string, can use len()
:
>>> s = 'please answer question' >>> len(s) # number of characters in s 25
if need size of string in bytes, need sys.getsizeof()
:
>>> import sys >>> sys.getsizeof(s) 58
also, don't call string str
. shadows built-in str()
function.
Comments
Post a Comment