1. Home
  2. Computing & Technology
  3. Python

Beginning Python: Python Encodings

From , former About.com Guide

4 of 7

How Python Prints Unicode

To output a Unicode string, we must first encode the Unicode string as a standard string. To do this, we use Python's encode function in order to tell Python which encoding to use. For the Unicode strings of the previous page, we could write:

a = x.encode("utf-8")
b = y.encode("utf-8")
c = z.encode("utf-8")
Then we can print them as usual or otherwise write them to a file-like object (e.g., local file, web page, etc.). Now that the values are assigned to other variables, we can reference those handles like any other variable:
>>> converted = (a, b, c)
>>> for i in converted:
...      print i
...
ש
Ù„
პ
This is one way of converting Unicode strings to standard strings in Python. There is, however, a much easier way. Go to the next page of this tutorial to find out how.

Explore Python
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Beginning Python
  5. Outputting Unicode with Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.