Working with Python 2.7, I'm wondering which real advantage has using type
# -*- coding: utf-8 -*-
a = 'á'
ua = u'á'
print a, ua
>>> a = 'á'
>>> ua = u'á'
unicode, which is python 3's
str, is meant to handle text. Text is a sequence of code points which may be bigger than a single byte. Text can be encoded in a specific encoding to represent the text as raw bytes(e.g.
unicode is not encoded! The internal representation used by python is an implementation detail, and you shouldn't care about it as long as it is able to represent the code points you want.
On the contrary
str is a plain sequence of bytes. It does not represent text! In fact, in python 3
str is called
You can think of
unicode as a general representation of some text, which can be encoded in many different ways into a sequence of binary data represented via
Some differences that you can see:
>>> len(u'à') # a single code point 1 >>> len('à') # by default utf-8 -> takes two bytes 2 >>> len(u'à'.encode('utf-8')) 2 >>> len(u'à'.encode('latin1')) # in latin1 it takes one byte 1 >>> print u'à'.encode('utf-8') # terminal encoding is utf-8 à >>> print u'à'.encode('latin1') # it cannot understand the latin1 byte �
Note that using
str you have a lower-level control on the single bytes of a specific encoding representation, while using
unicode you can only control at the code-point level. For example you can do:
>>> 'àèìòù' '\xc3\xa0\xc3\xa8\xc3\xac\xc3\xb2\xc3\xb9' >>> print 'àèìòù'.replace('\xa8', '') à�ìòù
What before was valid UTF-8, isn't anymore. Using a unicode string you cannot operate in such a way that the resulting string isn't valid unicode text. You can remove a code point, replace a code point with a different code point etc. but you cannot mess with the internal representation.