如何在Python中获取字符串的大小(长度)
例如,我得到一个字符串:
str = "please answer my question"
我想将其写入文件。
但在将字符串写入文件之前我需要知道字符串的大小。我可以使用什么函数来计算字符串的大小?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您还可以使用 str.len() 来计算列中元素的长度
You also may use str.len() to count length of element in the column
最Pythonic 的方法是使用len()。请记住,转义序列中的“\”字符不被计算在内,如果使用不当,可能会产生危险。
The most Pythonic way 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.你想求Python语言中字符串的长度吗?如果你想知道单词的长度,你可以使用len函数。
输出:
Do you want to find the length of the string in the Python language? If you want to find the length of the word, you can use the len function.
Output:
提供的答案很恰当,但值得注意的是,在 Python 中,空格也被视为字符。
但是,如果希望创建一个需要计算某人姓名中字符总数的函数,则可能需要删除空格。这可以使用
.replace()
方法轻松完成。例如:
这里的输出是12(不包括空格)而不是13(包括空格)。
The responses provided are apt, but it is important to note that in Python, blank spaces are also considered characters.
However, if one wishes to create a function that requires counting total characters in someone’s name for instance, it may be necessary to remove blank spaces. This can easily be accomplished using the
.replace()
method.For example:
The output here is 12 (excluding blank space) instead of 13 (including blank space).
如果您正在谈论字符串的长度,可以使用
len( )
:如果需要字符串的大小(以字节为单位),则需要
sys.getsizeof()
:另外,不要调用字符串变量
str
。它隐藏了内置的str()
功能。If you are talking about the length of the string, you can use
len()
:If you need the size of the string in bytes, you need
sys.getsizeof()
:Also, don't call your string variable
str
. It shadows the built-instr()
function.Python 3:
user225312的答案是正确的:
A.计算
中的字符数str
对象,您可以使用len()
函数:B. 要获取分配用于存储
str
对象的内存大小(以字节为单位),您可以使用 sys.getsizeof() 函数Python 2:
对于 Python 2 来说它变得复杂。
A. Python 2 中的 len() 函数返回分配用于在
str
对象中存储编码字符的字节数。有时它会等于字符数:
但有时,它不会:
那是因为
str
可以使用 内部可变长度编码。因此,要计算str
中的字符数,您应该知道str
对象正在使用哪种编码。然后你可以将其转换为unicode
对象并获取字符计数:B.
sys.getsizeof()
函数与 Python 3 中的功能相同 - 它返回分配用于存储整个字符串对象的字节数Python 3:
user225312's answer is correct:
A. To count number of characters in
str
object, you can uselen()
function:B. To get memory size in bytes allocated to store
str
object, you can usesys.getsizeof()
functionPython 2:
It gets complicated for Python 2.
A. The
len()
function in Python 2 returns count of bytes allocated to store encoded characters in astr
object.Sometimes it will be equal to character count:
But sometimes, it won't:
That's because
str
can use variable-length encoding internally. So, to count characters instr
you should know which encoding yourstr
object is using. Then you can convert it tounicode
object and get character count: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