how to get size of unicode string/string in bytes ?

Diez B. Roggisch deets at nospam.web.de
Tue Aug 1 05:13:55 EDT 2006


Stefan Behnel wrote:

> pattreeya at gmail.com wrote:
>>   how can I get the number of byte of the string in python?
>> with "len(string)", it doesn't work to get the size of the string in
>> bytes if I have the unicode string but just the length. (it only works
>> fine for ascii/latin1) In data structure, I have to store unicode
>> string for many languages and must know exactly how big of my string
>> which is stored so I can read back later.
> 
> I do not quite know what you could possibly need that for, but AFAICT
> Python only uses two different unicode encodings depending on the
> platform.

It is very important for relational databases, as these usually constrain
the amount of bytes per column - so you need the size of bytes, not the
number of unicode characters.

Diez



More information about the Python-list mailing list