byte count unicode string

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Sep 20 02:43:06 EDT 2006


In <mailman.313.1158732191.10491.python-list at python.org>, willie wrote:

> # What's the correct way to get the
> # byte count of a unicode (UTF-8) string?
> # I couldn't find a builtin method
> # and the following is memory inefficient.
> 
> ustr = "example\xC2\x9D".decode('UTF-8')
> 
> num_chars = len(ustr)    # 8
> 
> buf = ustr.encode('UTF-8')
> 
> num_bytes = len(buf)     # 9

That is the correct way.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list