1. Computing

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") 
 ... 
 ש 
 Ù„ 
 პ 

  1. About.com
  2. Computing
  3. Python
  4. Beginning Python
  5. Encoding Unicode Strings On-the-Fly with Python

©2013 About.com. All rights reserved.