Count characters of a String


It is correct to count characters of a string in the following way:

cadena = "este es un texto de prueba"
total_caracteres = len(cadena)
print total_caracteres

now the doubt arises when I have a text with an average of 10,000 characters or more that is to say I want to count both all-inclusive blanks. Maybe some guide or my way of counting them is correct.

Thanks in advance.

asked by Diego Avila 03.01.2019 в 20:43

1 answer


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'))

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'))

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'))

But another one does not:

>>> print(len('йцы')) # La cadena tiene tres caracteres cyrillicos

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

B. The len() function does the same as in Python 3 - returns the number of bytes allocated to store the entire string.

>>> print(getsizeof('йцы'))
>>> print(getsizeof('йцы'.decode('utf8')))
answered by 03.01.2019 / 21:08