如何在 Python 中获取字符串的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4967580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:23:23  来源:igfitidea点击:

How to get the size of a string in Python?

pythonstringstring-length

提问by babykick

For example, I get a string:

例如,我得到一个字符串:

str = "please answer my question"

I want to write it to a file.

我想把它写到一个文件中。

But I need to know the size of the string before writing the string to the file. What function can I use to calculate the size of the string?

但是在将字符串写入文件之前,我需要知道字符串的大小。我可以使用什么函数来计算字符串的大小?

采纳答案by user225312

If you are talking about the length of the string, you can use len():

如果你在谈论字符串的长度,你可以使用len()

>>> s = 'please answer my question'
>>> len(s)  # number of characters in s
25

If you need the size of the string in bytes, you need sys.getsizeof():

如果您需要以字节为单位的字符串大小,则需要sys.getsizeof()

>>> import sys
>>> sys.getsizeof(s)
58

Also, don't call your string variable str. It shadows the built-in str()function.

另外,不要调用您的字符串变量str。它隐藏了内置str()函数。

回答by Michal Chruszcz

>>> s = 'abcd'
>>> len(s)
4

回答by Igor Bendrup

Python 3:

蟒蛇3:

user225312's answeris correct:

user225312 的回答是正确的:

A.To count number of characters in strobject, you can use len()function:

A.要计算str对象中的字符数,您可以使用len()函数:

>>> print(len('please anwser my question'))
25

B.To get memory size in bytes allocated to store strobject, you can use sys.getsizeof()function

B.要获取分配给存储str对象的内存大小(以字节为单位),可以使用sys.getsizeof()函数

>>> from sys import getsizeof
>>> print(getsizeof('please anwser my question'))
50

Python 2:

蟒蛇2:

It gets complicated for Python 2.

对于 Python 2,它变得复杂。

A.The len()function in Python 2 returns count of bytes allocated to store encoded charactersin a strobject.

A.len()在Python功能2返回计数分配给商店的字节编码字符str对象。

Sometimes it will be equal to character count:

有时它会等于字符数:

>>> print(len('abc'))
3

But sometimes, it won't:

但有时,它不会:

>>> print(len('йцы'))  # String contains Cyrillic symbols
6

That's because strcan use variable-length encodinginternally. So, to count characters in stryou should know which encoding your strobject is using. Then you can convert it to unicodeobject and get character count:

那是因为str可以在内部使用变长编码。因此,要计算字符数,str您应该知道您的str对象正在使用哪种编码。然后您可以将其转换为unicode对象并获取字符数:

>>> print(len('йцы'.decode('utf8'))) #String contains Cyrillic symbols 
3

B.The sys.getsizeof()function does the same thing as in Python 3 - it returns count of bytes allocated to store the whole string object

B.sys.getsizeof()函数与 Python 3 中的功能相同 - 它返回分配用于存储整个字符串对象的字节数

>>> print(getsizeof('йцы'))
27
>>> print(getsizeof('йцы'.decode('utf8')))
32

回答by Robert Grossman

The most Pythonicway is to use the len(). Keep in mind that the '\' character in escape sequences is not counted and can be dangerous if not used correctly.

Pythonic 的方式是使用len(). 请记住,转义序列中的 '\' 字符不计算在内,如果使用不当可能会很危险。

>>> len('foo')
3
>>> len('\foo')
3
>>> len('\xoo')
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape

回答by Vladimir Gavrysh

You also may use str.len() to count length of element in the column

您也可以使用 str.len() 来计算列中元素的长度

data['name of column'].str.len()