Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replicate Python 2 style len() in Python 3?

I am looking to replicate Python 2 style len() in Python 3, as it relates to unicode strings.

In Python 2 the len() of a unicode string is its on-disk bytes size. for example: len("애정") returns 6. In Python 3 len() returns the number of characters in the string, the example returns 2.

sys.getsizeof() is not the solution, because that gets the size of the Python object in-memory, not the size the object would be if it was written to the disk.

like image 961
Ian McKenzie Avatar asked Jul 19 '19 15:07

Ian McKenzie


1 Answers

You can encode it to utf8 like below.

>>> len('애정'.encode('utf8'))
6
>>>
like image 154
Praveenkumar Avatar answered Oct 27 '22 16:10

Praveenkumar