2.3 Unicode Literals in Python Source Code In Python source code, specific Unicode code points can be written using the$_{\u}$ escape sequence, which is followed by four hex digits giving the code point. The$_{\U}$ escape sequence is similar, but expects eight hex digits, not four: Using escape sequences for code points greater than 127 is fine in small doses, but becomes an annoyance if you’re using many accented characters, as you would in a program with messages in French or some other accent-using language. You can also assemble strings using the chr() built-in function, but this is even more tedious. Ideally, you’d want to be able to write literals in your language’s natural encoding. You could then edit Python source code with your favorite editor which would display the accented characters naturally, and have the right characters used at runtime. Python supports writing source code in UTF-8 by default, but you can use almost any encoding if you declare the encoding being used. This is done by including a special comment as either the first or second line of the source file: If you don’t include such a comment, the default encoding used will be UTF-8 as already mentioned. See also PEP 263 for more information. The syntax is inspired by Emacs’s notation for specifying variables local to a file. Emacs supports many different variables, but Python only supports ‘coding’. The$_{-*-}$ symbols indicate to Emacs that the comment is special; they have no significance to Python but are a convention. Python looks for$_{coding: name}$ or$_{coding=name}$ in the comment. The Unicode specification includes a database of information about code points. For each defined code point, the information includes the character’s name, its category, the numeric value if applicable (for characters representing numeric concepts such as the Roman numerals, fractions such as one-third and four-fifths, etc.). There are also display-related properties, such as how to use the code point in bidirectional text. The following program displays some information about several characters, and prints the numeric value of one particular character: When run, this prints: 6 import unicodedata u =chr(233 )+chr(0x0bf2 )+chr(3972 )+chr(6000 )+chr(13231 ) for i, c in enumerate(u): print(i,' %04x '%ord(c), unicodedata .category(c), end ="") print(unicodedata .name(c)) # Get numeric value of second character print(unicodedata .numeric(u[1])) #!/usr/bin/env python # -*- coding: latin-1 -*- u ='abcdé' print(ord(u[-1])) >>> s ="a \xac\u1234 \u20ac \U00008000 " ... # ^^^^ two-digit hex escape ... # ^^^^^^ four-digit Unicode escape ... # ^^^^^^^^^^ eight-digit Unicode escape >>> [ord(c) for c in s] [97, 172, 4660, 8364, 32768] 2.4 Unicode Properties