Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获得字符串的大小?_Python - Fatal编程技术网

Python 如何获得字符串的大小?

Python 如何获得字符串的大小?,python,Python,我有一个TCP客户端-服务器通信。我正在发送字符串,我想测量包裹的大小,我怎么做 在我的情况下,这是不好的,因为 getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector. 一个字符的大小是多少,2字节还是4字节?在这种情况下,我可以简单地测量为msg_si

我有一个TCP客户端-服务器通信。我正在发送字符串,我想测量包裹的大小,我怎么做

在我的情况下,这是不好的,因为

getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector
overhead if the object is managed by the garbage collector.

一个字符的大小是多少,2字节还是4字节?在这种情况下,我可以简单地测量为
msg_size=x*len(msg)
,但我不知道在Python2.x中有多少字节是
x
str
对象是8位的,即每个项1字节。在3.x中,您可以使用
字节
对象,它们同样是每个项目1字节

对于处理数据,通常最好使用;这是可变的,在2.x和3.x中的作用相同。

len(string)返回字符串使用的字节数(len(ustring)返回字符数)