1. Home
  2. Computing & Technology
  3. Python

Beginning Python: Python Encodings

From , former About.com Guide

5 of 7

Python's Way of Encoding Unicode Strings "En Passant"

Another way to use the encode function is to convert the value on-the-fly, including it in the print statement.

print x.encode("utf-8")
print y.encode("utf-8")
print z.encode("utf-8")
If this is still too much typing, you will be glad to know that Python will take any object for the encode function, even an iterator. Therefore, one can also feed the encoding argument to a series of Unicode strings like this:
>>> convert_on_output = (x, y, z)
>>> for j in convert_on_output:
...      print j.encode("utf-8")
...
ש
Ù„
პ

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. Encoding Unicode Strings On-the-Fly with Python

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

All rights reserved.