|
Original |
Translation |
|
24
|
Combining 8-bit and Unicode strings always coerces to Unicode, using the default ASCII encoding; the result of ``'a' + u'bc'`` is ``u'abc'``.
|
|
|
25
|
New built-in functions have been added, and existing built-ins modified to support Unicode:
|
|
|
26
|
``unichr(ch)`` returns a Unicode string 1 character long, containing the character *ch*.
|
|
|
27
|
``ord(u)``, where *u* is a 1-character regular or Unicode string, returns the number of the character as an integer.
|
|
|
28
|
``unicode(string [, encoding] [, errors] )`` creates a Unicode string from an 8-bit string. ``encoding`` is a string naming the encoding to use. The ``errors`` parameter specifies the treatment of characters that are invalid for the current encoding; passing ``'strict'`` as the value causes an exception to be raised on any encoding error, while ``'ignore'`` causes errors to be silently ignored and ``'replace'`` uses U+FFFD, the official replacement character, in case of any problems.
|
|
|
29
|
|
30
|
A new module, :mod:`unicodedata`, provides an interface to Unicode character properties. For example, ``unicodedata.category(u'A')`` returns the 2-character string 'Lu', the 'L' denoting it's a letter, and 'u' meaning that it's uppercase. ``unicodedata.bidirectional(u'\u0660')`` returns 'AN', meaning that U+0660 is an Arabic number.
|
|
|
31
|
The :mod:`codecs` module contains functions to look up existing encodings and register new ones. Unless you want to implement a new encoding, you'll most often use the :func:`codecs.lookup(encoding)` function, which returns a 4-element tuple: ``(encode_func, decode_func, stream_reader, stream_writer)``.
|
|
|
32
|
*encode_func* is a function that takes a Unicode string, and returns a 2-tuple ``(string, length)``. *string* is an 8-bit string containing a portion (perhaps all) of the Unicode string converted into the given encoding, and *length* tells you how much of the Unicode string was converted.
|
|
|
33
|
*decode_func* is the opposite of *encode_func*, taking an 8-bit string and returning a 2-tuple ``(ustring, length)``, consisting of the resulting Unicode string *ustring* and the integer *length* telling how much of the 8-bit string was consumed.
|
|