python string how many bytes

Is there some functions can tell me how many bytes are occupied by the string in memory?

I need to set the size of the socket buffer, in order to transmit the entire first string.

 

solution


import sys
sys.getsizeof(s) # getsizeof(object, default) -> int # Return the size of object in bytes.

But in fact you need to know how long it represents, so something like that len(s)should be enough.

This article first appeared in Python black hole net , blog updated simultaneously Park

Guess you like

Origin www.cnblogs.com/pythonzhichan/p/11406839.html