Depends on several conditions, there is an excellent response here
Python 3. *:
A. To count the number of characters in a str
object, we can use the function len()
:
>>> print(len('please anwser my question'))
25
B. To get the reserved size in bytes to store the str
object, you can use sys.getsizeof()
:
>>> from sys import getsizeof
>>> print(getsizeof('please anwser my question'))
50
Python 2. *:
It's a bit more complicated for Python 2. *.
A. The len()
function in Python 2 returns the number of bytes reserved for storing encoded characters in a str
object.
Sometimes it will work fine:
>>> print(len('abc'))
3
But another one does not:
>>> print(len('йцы')) # La cadena tiene tres caracteres cyrillicos
6
This is because str
stores a variable length character encoding . So, to really count the characters you have to know first, what encoding is being used in the str
object. Then, it is possible to convert the string to an object unicode
and get the number of characters with% % co:
>>> print(len('йцы'.decode('utf8'))) # La cadena tiene tres caracteres cyrillicos
3
B. The len()
function does the same as in Python 3 - returns the number of bytes allocated to store the entire string.
>>> print(getsizeof('йцы'))
27
>>> print(getsizeof('йцы'.decode('utf8')))
32