Printing Unicode characters

Gerhard Häring gerhard.haering at gmx.de
Thu Aug 8 22:20:19 EDT 2002


* Blistex <me at privacy.net> [2002-08-07 16:30 -0700]:
> I'm testing how well my application handles Unicode characters and I have
> a snippet of code that looks like this:
> 
> for x in range(0x20, 0x7F):
> 	print u'\u%x' % (x)
> 

(You could use the unichr() function to convert integers to Unicode
characters.)

A unicode string is something abstract, you can't write it to a file or
to a terminal. Instead, you have to encode it into a normal (8-bit
string) to do so.

A common encoding for 8-bit strings that can represent all Unicode
characters is 'UTF-8'. So if you want to print Unicode strings like
above, you could use:

for x in range(0x20, 0x7F):
    print unichr(x).encode('utf-8')

There are lots of other supported encodings, it's up to you which one
you want to use.

HTH,

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))




More information about the Python-list mailing list